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 variablesone 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.