You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can create a code snippet with only a few steps. All you need to do is create an XML file, fill in the appropriate elements, and add your code to it. You can also add references and replacement parameters to your code. You can add the snippet to your Visual Studio installation by using the **Import** button on the **Code Snippets Manager** (**Tools** > **Code Snippets Manager**).
23
+
You can create a code snippet with only a few steps. All you need to do is create an XML file, fill in the appropriate elements, and add your code to it. You can optionally make use of replacement parameters and project references. Import the snippet to your Visual Studio installation by using the **Import** button in the **Code Snippets Manager** (**Tools** > **Code Snippets Manager**).
24
24
25
25
## Snippet template
26
26
27
-
The following is the basic snippet template:
27
+
The following XML is the basic snippet template:
28
28
29
29
```xml
30
30
<?xml version="1.0" encoding="utf-8"?>
@@ -42,227 +42,182 @@ The following is the basic snippet template:
42
42
</CodeSnippets>
43
43
```
44
44
45
-
###Create a code snippet
45
+
## Create a code snippet
46
46
47
47
1. Create a new XML file in Visual Studio and add the template shown above.
48
48
49
-
2. Fill in the title of the snippet, e.g. "Hello World VB", in the **Title** element.
49
+
2. Fill in the title of the snippetin the **Title** element. Use the title **Square Root**.
50
50
51
-
3. Fill in the language of the snippet in the **Language** attribute of the **Code** element. For this example, use "VB".
51
+
3. Fill in the language of the snippet in the **Language** attribute of the **Code** element. For C#, use **CSharp**, and for Visual Basic, use **VB**.
52
52
53
-
4. Add some code in the **CDATA** section inside the **Code** element, for example:
53
+
> [!TIP]
54
+
> To see all the available language values, browse the [Code element attributes section](code-snippets-schema-reference.md#attributes) on the [Code snippets schema reference](code-snippets-schema-reference.md) page.
54
55
55
-
```xml
56
-
<CodeLanguage="VB">
57
-
<![CDATA[Console.WriteLine("Hello, World!")]]>
58
-
</Code>
59
-
```
56
+
4. Add the snippet code in the **CDATA** section inside the **Code** element.
60
57
61
-
5. Save the snippet as *VBCodeSnippet.snippet*.
58
+
For C#:
62
59
63
-
### Add a code snippet to Visual Studio
60
+
```xml
61
+
<CodeLanguage="CSharp">
62
+
<![CDATA[double root = Math.Sqrt(16);]]>
63
+
</Code>
64
+
```
64
65
65
-
1. You can add your own snippets to your Visual Studio installation by using the Code Snippets Manager. Open the **Code Snippets Manager** (**Tools** > **Code Snippets Manager**).
66
+
Or for Visual Basic:
67
+
68
+
```xml
69
+
<CodeLanguage="VB">
70
+
<![CDATA[Dim root = Math.Sqrt(16)]]>
71
+
</Code>
72
+
```
73
+
74
+
5. Save the snippet as *SquareRoot.snippet* (you can save it anywhere).
75
+
76
+
## Import a code snippet
77
+
78
+
1. You can import a snippet to your Visual Studio installation by using the **Code Snippets Manager**. Open it by choosing **Tools** > **Code Snippets Manager**.
66
79
67
80
2. Click the **Import** button.
68
81
69
82
3. Go to the location where you saved the code snippet in the previous procedure, select it, and click **Open**.
70
83
71
84
4. The **Import Code Snippet** dialog opens, asking you to choose where to add the snippet from the choices in the right pane. One of the choices should be **My Code Snippets**. Select it and click **Finish**, then **OK**.
72
85
73
-
5. The snippet is copied to the following location:
86
+
5. The snippet is copied to one of the following locations, depending on the code language:
74
87
75
88
::: moniker range="vs-2017"
76
89
90
+
*%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\Visual C#\My Code Snippets*
77
91
*%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\Visual Basic\My Code Snippets*
78
92
79
93
::: moniker-end
80
94
81
95
::: moniker range=">=vs-2019"
82
96
97
+
*%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets*
83
98
*%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual Basic\My Code Snippets*
84
99
85
100
::: moniker-end
86
101
87
-
6. Test your snippet by opening a Visual Basic project and opening a code file. In the file choose **Snippets** > **Insert Snippet** from the right-click menu, then **My Code Snippets**. You should see a snippet named **My Visual Basic Code Snippet**. Double-click it.
102
+
6. Test your snippet by opening a C# or Visual Basic project. With a code file open in the editor, choose **Snippets** > **Insert Snippet** from the right-click menu, then **My Code Snippets**. You should see a snippet named **Square Root**. Double-click it.
88
103
89
-
`Console.WriteLine("Hello, World!")` is inserted in the code file.
104
+
The snippet code is inserted in the code file.
90
105
91
-
### Add description and shortcut fields
106
+
##Description and shortcut fields
92
107
93
108
::: moniker range="vs-2017"
94
109
95
-
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\Visual Basic\My Code Snippet\VBCodeSnippet.snippet*.
110
+
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2017\Code Snippets\\[Visual C# or Visual Basic]\My Code Snippet\SquareRoot.snippet*.
96
111
97
112
::: moniker-end
98
113
99
114
::: moniker range=">=vs-2019"
100
115
101
-
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual Basic\My Code Snippet\VBCodeSnippet.snippet*.
116
+
1. Description fields give more information about your code snippet when viewed in the Code Snippets Manager. The shortcut is a tag that users can type in order to insert your snippet. Edit the snippet you have added by opening the file *%USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\\[Visual C# or Visual Basic]\My Code Snippet\SquareRoot.snippet*.
102
117
103
118
::: moniker-end
104
119
120
+
> [!TIP]
121
+
> Since you're editing the file in the directory where Visual Studio placed it, you don't need to reimport it to Visual Studio.
122
+
105
123
2. Add **Author** and **Description** elements to the **Header** element, and fill them in.
106
124
107
125
3. The **Header** element should look something like this:
108
126
109
-
```xml
110
-
<Header>
111
-
<Title>Hello World VB</Title>
112
-
<Author>Myself</Author>
113
-
<Description>Says Hello to the world.</Description>
114
-
</Header>
115
-
```
127
+
```xml
128
+
<Header>
129
+
<Title>Square Root</Title>
130
+
<Author>Myself</Author>
131
+
<Description>Calculates the square root of 16.</Description>
132
+
</Header>
133
+
```
116
134
117
-
4. Open the **Code Snippets Manager** and select your code snippet. In the right pane you should see that the **Description** and **Author** fields are now populated.
135
+
4. Open the **Code Snippets Manager** and select your code snippet. In the right pane, notice that the **Description** and **Author** fields are now populated.
118
136
119
-
5. To add a shortcut, add a **Shortcut** element alongside the **Author** and **Description** element:
137
+

120
138
121
-
```xml
122
-
<Header>
123
-
<Title>Hello World VB</Title>
124
-
<Author>Myself</Author>
125
-
<Description>Says Hello to the world.</Description>
126
-
<Shortcut>hello</Shortcut>
139
+
5. To add a shortcut, add a **Shortcut** element within the **Header** element:
140
+
141
+
```xml
142
+
<Header>
143
+
<Title>Square Root</Title>
144
+
<Author>Myself</Author>
145
+
<Description>Calculates the square root of 16.</Description>
146
+
<Shortcut>sqrt</Shortcut>
127
147
</Header>
128
-
```
148
+
```
129
149
130
150
6. Save the snippet file again.
131
151
132
-
7. To test the shortcut, open a Visual Basic project and open a code file. Type `hello` in the file and press **Tab** twice.
133
-
134
-
The snippet code is inserted.
135
-
136
-
### Add references and imports
137
-
138
-
1. You can add a reference to a project by using the **References** element, and add an Imports declaration by using the **Imports** element. (This works for C# as well.) For example, if you change `Console.WriteLine` in the code example to `MessageBox.Show`, you may need to add the *System.Windows.Forms.dll* assembly to the project.
152
+
7. To test the shortcut, open the project you used previously, type **sqrt** in the editor and press **Tab** (once for Visual Basic, twice for C#).
139
153
140
-
2. Open your snippet.
154
+
The snippet code is inserted.
141
155
142
-
3. Add the **References** element under the **Snippet** element:
156
+
## Replacement parameters
143
157
144
-
```xml
145
-
<References>
146
-
<Reference>
147
-
<Assembly>System.Windows.Forms.dll</Assembly>
148
-
</Reference>
149
-
</References>
150
-
```
158
+
You may want parts of a code snippet to be replaced by the user. For example, you might want the user to replace a variable name with one in their current project. You can provide two types of replacements: literals and objects. Use the [Literal element](code-snippets-schema-reference.md#literal-element) to identify a replacement for a piece of code that is entirely contained within the snippet but will likely be customized after it's inserted into the code (for example, a string or numeric value). Use the [Object element](code-snippets-schema-reference.md#object-element) to identify an item that's required by the code snippet but is likely to be defined outside of the snippet itself (for example, an object instance or a control).
151
159
152
-
4. Add the **Imports** element under the **Snippet** element:
160
+
1. To enable the user to easily replace the number to calculate the square root of, modify the **Snippet** element of the *SquareRoot.snippet* file as follows:
153
161
154
-
```xml
155
-
<Imports>
156
-
<Import>
157
-
<Namespace>System.Windows.Forms</Namespace>
158
-
</Import>
159
-
</Imports>
160
-
```
161
-
162
-
5. Change the **CDATA** section to the following:
162
+
```xml
163
+
<Snippet>
164
+
<CodeLanguage="CSharp">
165
+
<![CDATA[double root = Math.Sqrt($Number$);]]>
166
+
</Code>
167
+
<Declarations>
168
+
<Literal>
169
+
<ID>Number</ID>
170
+
<ToolTip>Choose the number you want the square root of.</ToolTip>
171
+
<Default>16</Default>
172
+
</Literal>
173
+
</Declarations>
174
+
</Snippet>
175
+
```
163
176
164
-
```xml
165
-
<![CDATA[MessageBox.Show("Hello, World!")]]>
166
-
```
177
+
Notice that the literal replacement is given an ID (`Number`). That ID is referenced from within the code snippet by surrounding it with `$` characters:
167
178
168
-
6. Save the snippet.
179
+
```xml
180
+
<![CDATA[double root = Math.Sqrt($Number$);]]>
181
+
```
169
182
170
-
7. Open a Visual Basic project and add the snippet.
183
+
2. Save the snippet file.
171
184
172
-
8. You will see an `Imports` statement at the top of the code file:
185
+
3. Open a project and insert the snippet.
173
186
174
-
```vb
175
-
Imports System.Windows.Forms
176
-
```
187
+
The code snippet is inserted and the editable literal is highlighted for replacement. Hover over the replacement parameter to see the tooltip for the value.
177
188
178
-
9. Look at the project's properties. The **References** tab includes a reference to *System.Windows.Forms.dll*.
189
+

179
190
180
-
### Add replacements
191
+
> [!TIP]
192
+
> If there's more than one replacable parameter in a snippet, you can press **Tab** to navigate from one to the other to change the values.
181
193
182
-
1. You may want parts of your code snippets to be replaced by the user, for example if you add a variable and want the user to replace the variable with one in the current project. You can provide two types of replacements: literals and objects. Literals are strings of some type (string literals, variable names, or string representations of numeric values). Objects are instances of some type other than a string. In this procedure you will declare a literal replacement and an object replacement, and change the code to reference these replacements.
194
+
## Import a namespace
183
195
184
-
2. Open your snippet.
196
+
You can use a code snippet to add a `using` directive (C#) or `Imports` statement (Visual Basic) by including the [Imports element](code-snippets-schema-reference.md#imports-element). For .NET Framework projects, you can also add a reference to the project by using the [References element](code-snippets-schema-reference.md#references-element).
185
197
186
-
3. This example uses a SQL connection string, so you need to change the **Imports** and **References** elements to add the appropriate references:
198
+
The following XML shows a code snippet that uses the method `File.Exists` in the System.IO namespace and, therefore, defines the **Imports**element to import the System.IO namespace.
4. To declare a literal replacement for the SQL connection string, add a **Declarations** element under the **Snippet** element, and in it add a **Literal** element with sub-elements for the ID, the tooltip, and the default value for the replacement:
208
-
209
-
```xml
210
-
<Declarations>
211
-
<Literal>
212
-
<ID>SqlConnString</ID>
213
-
<ToolTip>Replace with a SQL connection string.</ToolTip>
214
-
<Default>"SQL connection string"</Default>
215
-
</Literal>
216
-
</Declarations>
217
-
```
218
-
219
-
5. To declare an object replacement for the SQL connection, add an **Object** element inside the **Declarations** element, and add sub-elements for the ID, the type of the object, the tooltip, and the default value. The resulting **Declarations** element should look like this:
220
-
221
-
```xml
222
-
<Declarations>
223
-
<Literal>
224
-
<ID>SqlConnString</ID>
225
-
<ToolTip>Replace with a SQL connection string.</ToolTip>
226
-
<Default>"SQL connection string"</Default>
227
-
</Literal>
228
-
<Object>
229
-
<ID>SqlConnection</ID>
230
-
<Type>System.Data.SqlClient.SqlConnection</Type>
231
-
<ToolTip>Replace with a connection object in your application.</ToolTip>
232
-
<Default>dcConnection</Default>
233
-
</Object>
234
-
</Declarations>
235
-
```
236
-
237
-
6. In the code section, you reference the replacements with surrounding $ signs, for example `$replacement$`:
238
-
239
-
```xml
240
-
<CodeLanguage="VB"Kind="method body">
241
-
<![CDATA[Dim daCustomers As SqlDataAdapter
242
-
Dim selectCommand As SqlCommand
243
-
244
-
daCustomers = New SqlClient.SqlDataAdapter()
245
-
selectCommand = new SqlClient.SqlCommand($SqlConnString$)
8. Open a Visual Basic project and add the snippet.
254
-
255
-
9. The code should look like the following, where the replacements `SQL connection string` and `dcConnection` are highlighted in light orange. Choose **Tab** to navigate from one to the other.
256
-
257
-
```vb
258
-
Dim daCustomers As SqlDataAdapter
259
-
Dim selectCommand As SqlCommand
260
-
261
-
daCustomers = New SqlClient.SqlDataAdapter()
262
-
selectCommand = New SqlClient.SqlCommand("SQL connection string")
0 commit comments