Build Snippets with Code Snippet Editor for Visual Basic 2005by Wei-Meng Lee
Sometime back in 2003, I wrote an article on how to create your own code snippet for Visual Studio 2005 (codenamed Whidbey) by hand using XML. However, if you prefer not to dirty your hands writing XML code, a better option is to use the Code Snippet Editor for Visual Basic 2005, a shared source project developed by the Visual Basic developer community. Using the Code Snippet Editor for Visual Basic 2005, you can now add your own code snippet easily without needing to know how code snippets are stored and processed.
Obtaining the Code Snippet Editor for Visual Basic 2005
After the installation, the Code Snippet Editor for Visual Basic 2005 will be installed in the following directory: C:\Documents and Settings\<user name>\My Documents\MSDN\Code Snippet Editor\. Double-click the SnippetEditor.exe file to launch the Code Snippet Editor for Visual Basic 2005 (see Figure 1).
Figure 1. The Code Snippet Editor for Visual Basic 2005
You can view the list of code snippets already installed on your system by expanding the tree on the left panel of the Code Snippet Editor for Visual Basic 2005 (see Figure 2).
Figure 2. Viewing the list of code snippets currently installed on your computer
Creating Your Own Code Snippet
To create your own code snippet, first choose the folder you want to add it to and then right-click on it. You can create a new folder under the selected folder, or create a snippet under the selected folder. Let's create a new folder under the filesystem folder and name it FTP (see Figure 3).
Figure 3. Creating a new folder (FTP) under the filesystem folder
The FTP folder will now be created under the filesystem folder. Right-click on the FTP folder and select Add New Snippet to add a new snippet to the FTP folder. Name the new snippet Download a Text File (see Figure 4).
Figure 4. Adding a new snippet to the FTP folder
You should now see the new snippet under the FTP folder (see Figure 5).
Figure 5. The new snippet appears under the FTP folder
Double-click the newly created snippet and enter the following block of code into the Editor pane on the right of the window:
Try Dim path As String = ftpURI & filename Dim ftpReq As FtpWebRequest = WebRequest.Create(path) ftpReq.Method = WebRequestMethods.Ftp.DownloadFile ftpReq.Credentials = New NetworkCredential(username, password) Dim ftpResp As FtpWebResponse = ftpReq.GetResponse Dim ftpRespStream As Stream = FTPResp.GetResponseStream Dim reader As StreamReader reader = New StreamReader(ftpRespStream, System.Text.Encoding.UTF8) Console.WriteLine(reader.ReadToEnd) ftpResp.Close() Catch ex As Exception Console.WriteLine(ex.ToString) End Try
Enter the rest of the information as shown in the Properties tab (see Figure 6).
Figure 6. Entering the values of the various properties of the code snippet
At the moment, only the Visual Basic language is supported. Also note that there are four available scopes that you can choose:
- Class Declaration: The code snippet is a class declaration.
- Member Declaration: The code snippet is a method declaration.
- Member body: The code snippet is to be inserted into a method definition.
- Unspecified: The code snippet contains a mixture of the above three types.
The Scope option is only needed if you want to test the code snippet in the Code Snippet Editor for Visual Basic 2005. The next step is to insert the replacements so that users can customize the code for their own purposes. First, select the variable/constant you want to replace and click on the "Add new" button (see Figure 7).
Figure 7. Adding a replacement to the code snippet
Then, set its default value and its kind, as shown in Figure 8.
Figure 8. Setting the values for the replacement
Repeat the above steps for the other variables as shown in Figure 9 (those highlighted in yellow).
Figure 9. Adding the other replacements
Set the values of the properties as shown in Table 1.
Table 1. The values for the various replacements
|$filename$||filename||"file.txt"||The name of the file to download|
|$username$||username||"anonymous"||Username for FTP server|
|$password$||password||"password"||The password for the user|
In the References tab, you can add the references to the assemblies needed by your code snippet (see Figure 10).
Figure 10. Adding references to assemblies
In the Imports tab, you can import the namespaces required by your code snippet (see Figure 11).
Figure 11. Importing namespaces
Finally, you can test the code snippet to verify that it works (see Figure 12).
Note: For my example, you can ignore the errors shown.
Figure 12. Testing the code snippet
You can preview how the code snippet will look using the Preview pane (see Figure 13).
Figure 13. Previewing the code snippet
Once the code snippet is finished, you can save it by clicking the Save button located at the top of the editor (see Figure 14).
Figure 14. Saving the code snippet
To verify that the code snippet is indeed added to Visual Studio 2005, create a new Windows application and in the code editor, right-click and select Insert Snippet... (see Figure 15).
Figure 15. Inserting a code snippet
Navigate to File system - Process Drives, Folders, and Files | FTP and select Download a text file (see Figure 16).
Figure 16. Testing the code snippet
Figure 17 shows the code snippet inserted.
Figure 17. The inserted code snippet
Exporting the Code Snippet to VSI
While the Code Snippet Editor for Visual Studio 2005 automatically adds the code snippet to Visual Studio 2005, you need to manually install the code snippet to the other computers running Visual Studio 2005. To do so, first you need to export the Code Snippet using the Export button located at the top of the editor (see Figure 18).
Figure 18. Exporting the code snippet
You will be asked to provide a name and location to store the code snippet. The code snippet will then be saved as a Visual Studio Community Content Installer file (.vsi). To install the code snippet on other computers, simply copy this file to the target computer.
To install the code snippet on the target computer, double-click on the .vsi file to start the installation process (see Figure 19).
Figure 19. Installing the code snippet
You will be asked to select a location to install the code snippet (just check the folder; see Figure 20).
Figure 20. Selecting the location to install the code snippet
You can verify that the code snippet is installed correctly by trying it out in Visual Studio 2005 (see Figure 21).
Figure 21. Testing the installed code snippet
The Code Snippet Editor for Visual Basic 2005 greatly simplifies the task of creating code snippets. This is a useful tool to use if you are working as part of a development team and want to easily share commonly used code snippets with your fellow colleagues.
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to the Windows DevCenter.