blah blah blah is here! blah blah » Close

up0down
link

Hi,
Is there a way to create my own snipplet ? I mean a block of code that i m using very often !
Thanks
David

last answered 2 years ago

1 answers

up0down
link

I forgot!
I'm using VStudio 2008

up0down
link

Check out <a target="_new" href="http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=76651">this recent thread</a> for some ideas in this regard.

up0down
link

thanks

up0down
link

It's a shame they don't make this very easy to do in visual studio. I'm hoping they added support to easily create snippets in 2010 and hopefully in the express editions. The post vulpes links to mostly talks about writing reusable libraries which might not be what you are looking for.
First let me point you to MSDN's <a href="http://msdn.microsoft.com/en-us/library/ms165393.aspx>Creating Code Snippets</a> page for a full overview. And here's an example of a simple expansion snippet which I use all the time, the DOS style pause prompt:
<b>[Console.snippet]</b>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Console Pause</Title>
<Shortcut>pause</Shortcut>
<Description>Code snippet to have the classic console pause prompt.</Description>
<Author>Jeff</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
Console.WriteLine();]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
You can name the snippet file to anything you want. I named it for it's intended application type (Console.snippet). Just place the file in your snippets folder. By default, it should go to: "%USERPROFILE%\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets\". And you can add as many snippets in the file. I don't really do much of the more complex snippets like the replacement snippets, I'm more comfortable typing them all out by hand. The element names should speak for themselves. But this should be enough for you to get started writing your simple snippets.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback