3 out of 11 rated this helpful - Rate this topic

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. For more information on the Code Snippet XML schema, see Code Snippets Schema Reference.

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. Create an XML file, and then open it in the editor.

  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:

    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
    
  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").

    <Code Language="VB">
        <![CDATA[MessageBox.Show("Hello World")]]>
    </Code>
    
  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.

    Note Note

    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. From the File menu, click Save <filename> As…, and save the file in Drive:\...\Documents\Visual Studio Version\Code Snippets\Language\with a .snippet extension.

    For example, you can save the file according to the following path and name: \...\Documents\Visual Studio 2010\Code Snippets\Visual Basic\HelloWorldMsgBox.snippet

You can use this IntelliSense Code Snippet in Visual Studio by following the procedures in How to: Manage Code Snippets and How to: Insert IntelliSense Code Snippets.

This example contains the entire IntelliSense Code Snippet created 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>

This example contains a Visual C# version of the IntelliSense Code Snippet created in the previous steps. Visual C# code snippets to do not support the References section, so a reference to System.Windows.Forms.dll must be added to the project manually.

<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>
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Inserting Code Snippets
If the XML file is mandatory to create, then why not any template on "Add New Item"? This can be done on future release. One more thing, where to save the XML file?
Where do you save the file to?
Not sure where we should save the file (path)
Suggestion for future versions
Why not add an item to the code window context menu with the text: "Add as Snippet", so that highlighted code can easily be stored for future retrieval, instead of this extraordinarily verbose method?
Advertisement