Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Give the Visual Studio Snippet Format a Whirl with 10 Pre-packaged Snippets : Page 2

You aren't limited to using the snippets that ship with Visual Studio .NET 2005; you can convert your own. As a start, here are two in-depth examples showing how to convert existing tip code to snippets, as well as eight other useful tips—already "snippetized" and ready for you to use.




Application Security Testing: An Integral Part of DevOps

Tip 2: Creating and Deleting Directories
This next snippet was chosen to show additional features supported in snippet files and it's also an example of including multiple related snippets in a single file. This snippet contains three functions:

Imports System.IO Public Function CreateDirectory( _ ByVal FullPath As String) As Boolean Dim objDI As New DirectoryInfo(FullPath) Try objDI.Create() Return True Catch Return False End Try End Function Public Function CreateSubDirectory( _ ByVal ParentPath As String, _ ByVal NewSubDirectory As String) As Boolean Dim objDI As New DirectoryInfo(ParentPath) Try objDI.CreateSubdirectory(NewSubDirectory) Return True Catch Return False End Try End Function Public Function DeleteDirectory( _ ByVal FullPath As String) As Boolean Dim objDI As New DirectoryInfo(FullPath) Try objDI.Delete(True) Return True Catch Return False End Try End Function

You can find the original tip on FreeVBCode.

This snippet's header elements have two keywords: "File System" and "Directories" and I've assigned a shortcut of "createDelDir."

Begin by creating an opening <Snippet> element, which is the key to adding multiple snippets in one file. Each snippet resides in its own <Snippet> element. The functions reference the System Namespace, so you must add a <References> element, which contains a <Reference> child element for each referenced assembly. When you insert a snippet that has external references, VS adds the proper references to your project. In the <Reference> element add an <Assembly> element with a value of "System.dll," as shown below:

<References> <Reference> <Assembly>System.dll</Assembly> </Reference> </References>

The snippets also import the System.IO namespace, so below the closing </References> tag, add an <Imports> element with an <Import> child element, which should contain a <Namespace> element with a value of "System.IO." When you insert your snippet, this feature adds an Imports statement at the top of your VB file using the <Namespace> value as the identifier. Here's an example:

<Imports> <Import> <Namespace>System.IO</Namespace> </Import> </Imports>

With references and imports taken care of, the remainder of the process is similar to the first example. Create a <Declarations> element. This block of code has two variables—one literal string parameter and one local object reference (the DirectoryInfo object defined within the method)—so you'll have one <Literal> element and one <Object> element.

The parameter FullPath is the physical path the snippet uses when creating the new directory. Create the following elements in a process that by now should begin to look familiar.

  • An <ID> element with a value of PhysPath. This value represents the highlighted replaceable parameter in the snippet and VS inserts it into the actual code with dollar signs.
  • A <Type> element with a value of String, which declares the data type for this replaceable parameter.
  • A <ToolTip> element with a value of "Physical path to where you want to create a directory."
  • A <Default> element of "FullPath." This will be inserted into your code wherever VS finds the value of the <ID> element.
Your <Literal> node should now look like this:

<Literal> <ID>physPath</ID> <Type>String</Type> <ToolTip>Physical path to where you want to create a directory</ToolTip> <Default>FullPath</Default> </Literal>

Next, create the object reference by adding an <Object> element. The <Object> element has the same child elements as the <Literal> element.

<Object> <ID>dirInfoObj</ID> <Type>System.IO.DirectoryInfo</Type> <ToolTip>Replace with a Directory Info object</ToolTip> <Default>objDI</Default> </Object>

Note that although all three functions in this snippet file reference the System.IO.DirectoryInfo object, you only have to add the <Object> element once, in the first snippet. The same rule applies to the <Imports> and <References> elements; otherwise, VS would create duplicate Imports statements. Keep in mind also that if you have two <Literal> elements with the same ID, Visual Studio will use the properties you set in the first occurrence for each subsequent reference as well.

Now you can close the <Declarations> element and begin the <Code> element. Add Language="VB" and Kind="function body" attribute/value pairs. The value of your <Code> element will be the function code wrapped in a <![CDATA[]]> tag. Even though CDATA tells XML to not parse the characters within, Visual Studio parses the snippets upon insert, so it can process replaceable parameters delimited by dollar signs correctly.

Replace your variable reference with the value of your <ID> element, surrounded with dollar signs, and the object reference with the ID value of your <Object> element. The completed <Code> element should look like this,

<Code Language="VB" Kind="function body"> <![CDATA[ Public Function CreateDirectory( _ ByVal $physPath$ As String) As Boolean Dim? $dirInfoObj$ As New _ DirectoryInfo($physPath$) Try $dirInfoObj$.Create() Return True Catch Return False End Try $dirInfoObj$ = nothing End Function]]> </Code>

With that completed, you can now close your first <Snippet> element and open the second of the three <Snippet> elements. Follow the exact same procedures explained above to snippetize the remaining functions, CreateSubdirectory() and DeleteSubdirectory(). Listing 2 shows the completed three-snippet snippets file.

I won't bore you by repeating the process to create snippets out of the remaining tips. You can use these tips to practice the process of creating snippets from existing code, or simply download them with the rest of the sample code for this article and install them into Visual Studio, making them available for use in your projects.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date