Create a Snippet in Visual Studio 2010
Creating a Snippet in Visual Studio 2010
- Add a new XML file to your project and name it sampleHeader.snippet (Visual Studio snippets always have the extension .snippet).
- Right-click on the editor window and select Insert Snippet to create a basic snippet template. Since we are creating an Expansion Snippet, remove the tag : <snippetType>SurroundsWith</snippetType>
- Alter the Title tag to read “Code File Header.”
- Change the Author tag to your own name.
- Change the Shortcut tag (which is the trigger word that activates your snippet) to “vstutor”
- Enter a description for this snippet, for example “Adds header to the code file”.
- Since we are creating a C# snippet alter the Language attribute of the Code tag so it reads <Code Language=”CSharp”> . Snippets can be creating for any language such as Visual Basic, XML etc
- The Code section contains the code block to be added. In this example, modify the code section till it reads:<Code Language=”CSharp”><![CDATA[***********************************
Author: $Author$
Date:
Version:
Purpose:
***********************************
]]>
</Code>
The finished version of the snippet appear as below:
<CodeSnippet Format=”1.0.0″ xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>
<Header>
<Title>Code File Header</Title>
<Author>Test Author</Author>
<Shortcut>vstutor</Shortcut>
<Description>Adds a header to a code file</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Author</ID>
<Default>Alex Mackey</Default>
</Literal>
</Declarations>
<Code Language=”CSharp”>
<![CDATA[
***********************************
Author: $Author$
Date:
Version:
Purpose:
***********************************
]]>
</Code>
</Snippet>
</CodeSnippet>
Load Snippets into Visual Studio
Before the snippet can be used it will need to be loaded in Visual Studio. It is best practice to create a folder to hold all your snippets, to do this and load the folder follow the below steps:
Array



28. Feb, 2010 
No comments yet... Be the first to leave a reply!