Export (0) Print
Expand All

How to: Create a Basic Code Snippet

IntelliSense Code Snippets are XML files with a .snippet file name extension that adhere to the IntelliSense Code Snippet XML schema. In this topic, you will create a basic code snippet that displays a message box.

TipTip

The CodePlex website has tools such as Snippet Editor that can help you write code snippets.

To create a snippet file, you must create an XML file, and then write the XML code that makes up your code snippet. For more information on any of the XML elements used in the following examples, see Code Snippets Schema Reference.

To create a snippet file

  1. In Visual Studio, create a new XML file.

  2. Below the automatically generated line of XML, add a CodeSnippets element with the proper xmlns attribute value, and a CodeSnippet element to create an individual code snippet. For example:

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
    
    
      </CodeSnippet>
    </CodeSnippets>
    
    
  3. Add a header section to the code snippet. For example:

    <Header>
        <Title>
            My Snippet
        </Title>
    </Header>
    
  4. Add the elements that define the code snippet itself. In the following example, the language of the code snippet is Visual Basic.

    <Snippet>
        <Code Language="VB">
    
        </Code>
    </Snippet>
    
    NoteNote

    The Language attribute also accepts the values CSharp, VJSharp, and XML. For more information, see Code Element (IntelliSense Code Snippets).

  5. Inside the Code element, add the Visual Basic code for the snippet. All snippet code must be placed between <![CDATA[ and ]]> brackets. In the following example, the code that will be inserted is MessageBox.Show("Hello World").

    <![CDATA[MessageBox.Show("Hello World")]]>
    
    
  6. Inside the Snippet element, add the References element and all of the required child elements that add a reference to the project when the snippet is inserted. In this example, the code snippet adds a reference to System.Windows.Forms.dll when the snippet is inserted.

    NoteNote

    Only Visual Basic code snippets support the References section. Code snippets in other languages require that the correct references be added manually.

    <Snippet>
        <References>
            <Reference>
                <Assembly>System.Windows.Forms.dll</Assembly>
            </Reference>
        </References>
        <Code Language="VB">
            <![CDATA[MessageBox.Show("Hello World")]]>
        </Code>
    </Snippet>
    
  7. Save the file as %USER_PROFILE%\My Documents\Visual Studio Version\Code Snippets\Visual Basic\HelloWorld.snippet. All snippet files must have a .snippet extension.

You should see this snippet in the Code Snippets Manager (on the Tools / Options menu). On the drop-down list at the top, set the language to Visual Basic, and look in the My Code Snippets folder.

Here is the complete Visual Basic code snippet that you completed in the previous steps.

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>
                My Snippet
            </Title>
        </Header>
        <Snippet>
            <References>
                <Reference>
                    <Assembly>System.Windows.Forms.dll</Assembly>
                </Reference>
            </References>
            <Code Language="VB">
                <![CDATA[MessageBox.Show("Hello World")]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Here is a Visual C# version of the code snippet created in the previous steps. Visual C# code snippets to do not support the References section, so you need to add reference to System.Windows.Forms.dll to the project later on.

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>
                My Snippet
            </Title>
        </Header>
        <Snippet>
            <Code Language="CSharp">
                <![CDATA[MessageBox.Show("Hello World");]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Community Additions

ADD
Show:
© 2014 Microsoft