Monday, April 16, 2012

Create a code snippet in Visual Studio 2010

As a developer, your main rule should be Automate, Automate, Automate.
So, if you find yourself adding the same code over and over again, that sounds like the perfect place to automate.

A recent task I had to do recently was to insert a copyright text into all files touched. There is a very easy way to do all this.

Simply place the following text into a file called Copyright.snippet.


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0"
             xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
       <Header>
              <Title>Copyright Text</Title>
              <Author>reubenb</Author>
              <Shortcut>copyright</Shortcut>
              <Description>Adds the copyright text</Description>
              <SnippetTypes>
                     <SnippetType>Expansion</SnippetType>
              </SnippetTypes>
       </Header>
       <Snippet>
              <Code Language="CSharp">
                     // Copyright (c) 2012 Reuben Bartolo. All Rights Reserved.
              </Code>
       </Snippet>
</CodeSnippet>


Then place this file under the folder C:\Users\<userName>\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets\


Obviously adapt this to your own username and Visual Studio Version.


..and that's it, you are done. Now simply type copyright TAB TAB into your Visual Studio, and you are done!

You can manage all snippets by pressing Ctrl+K Ctrl+B.




No comments: