Visual Studio
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.

Creating an .snippet File

Before you can begin writing your code snippet, you must create an XML file with a .snippet file name extension.

To create a .snippet file

  1. On the File menu, click New and then click File.

  2. Click XML File and then click Open.

  3. On the File menu, click Save <XMLFileName>.

  4. In the Save as type box, select All Files (*.*).

  5. In the File name box, enter a file name with the .snippet file name extension.

  6. Click Save.

Writing the Code

Now that you have an XML file, you need to 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 write the code for the code snippet

  1. 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/2008/CodeSnippet">
        <CodeSnippet Format="1.0.0">
    
  2. Add a header section to the code snippet. For example:

    <Header>
        <Title>
            My Snippet
        </Title>
    </Header>
    
  3. 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).

  4. 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>
    
  5. 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>
    

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

Example

This example contains the entire IntelliSense Code Snippet created in the previous steps.

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2008/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/2008/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>
See Also

Concepts

Reference

Tags :


Community Content

Stanley Roark
documentation not updated ~~ 2005 should be 2008

You can now use this IntelliSense Code Snippet in Visual Studio 2005 ------------ should be 2008
by following the procedures in How to: Manage Code Snippets and
How to: Insert IntelliSense Code Snippets.

There are many freelance proof readers who would be happy to hunt down errors like this
in exchange for a reasonable fee. Of course, there are programmers like me doing the
same task for free. I'm not bitter but I wish whoever is updating these pages would be
more careful because there are still a lot of *real* vs2005 pages out there too. It's a
bit easy to get them mixed up when one gets used to seeing 2005 in places where it
should read 2008. Please be more careful. Thank you.

Tags : contentbug

Stanley Roark
Use a tool - Snippy
So for those of us who think it is too much work to write out XML files for snippets, there is a nice tool available that will do a good job.
http://www.codeplex.com/snippy

Check it out!

Page view tracker