Creating Downloadable Content


You can also easily create your own content to share with others. A .vsi file contains one or more files that are compressed using the .zip file format with the extension renamed from .zip to .vsi. In addition to the installable content files, the .vsi file contains a manifest file with the .vscontent extension. The .vscontent file uses the XML format and specifies not only which files to install, but also how they are to be installed. When the Content Installer opens the .vsi file, it first decompresses the file and searches for the first available file with the .vscontent extension. If such a file is present, it is opened, the XML within it is read, and then the Content Installer wizard appears, ready to start installing.

The VSContent File Format

The .vscontent file schema was designed to be very simple and easy to create. The XML file for content starts with the tag <VSContent>. This tag has one attribute that determines the XSD schema used not only for ensuring that the XML is correct, but also by the Visual Studio XML editor to give statement completion information when the file is opened for editing. The contents of the most basic .vscontent file are as follows:

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005"> </VSContent> 

Next we need to specify the items that will be installed. Each content item is enclosed within a tag named <Content> and lists the name and description of the item to display to the user with the <DisplayName> and <Description> tags, respectively. These tags contain any text that you want to display to the user identifying the content in the first page of the Content Installer—but keep it short because there is not much space available for display. To identify the type of content to the Content Installer, the <FileContentType> and <ContentVersion> tags are used. <FileContentType> can contain one of the following values: Toolbox Control, Macro Project, Addin, VSTemplate, or Code Snippet. <ContentVersion> specifies the version of the content to be installed and is generally the string "1.0." The last required tag (or tags, because you can specify more than one of them) is the <FileName> tag. This tag lists the files to install for the content item. The file names listed in <FileName> tags are relative to the location of the .vscontent file. If a file to install (let's suppose the file name is File. xyz) is zipped from the same directory as the .vscontent file, the file name is File.xyz; if the file is stored in a subdirectory of the folder containing the .vscontent file, the file name is SubDirectoryName\File.xyz.

When all of these tags are put together with a list of the files that are to be installed, you have a .vscontent file that can be loaded by the Content Installer to install on your computer or somebody else's. An example of a .vscontent file, which can be used to install controls onto the Toolbox, is given here:

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">  <Content>   <FileName>CompanyName\Control.dll</FileName>   <DisplayName>Test control</DisplayName>   <Description>A control to test with</Description>   <FileContentType>Toolbox Control</FileContentType>   <ContentVersion>1.0</ContentVersion>  </Content> </VSContent> 

Figure 4-1 showed a screenshot of the Content Installer with this .vscontent file open. Here, the Control.dll file is stored in a folder named CompanyName, whereas the .vscontent file is assumed to be in the same folder containing the folder CompanyName. You can combine multiple Content items within a .vscontent file, allowing you to distribute multiple items at one time or to group items that are related and dependent on one another.

Installing Templates and Starter Kits

Creating new projects or new files that go within projects is one of the most common, and yet more complicated, tasks that a developer may perform. If you needed to create a file with a simple class within that file, the task isn't overly complex—you simply add a new blank file to the project and add the class. However, imagine having to create a Windows form from scratch without a starting file generated by a wizard, and you can see how hard it would be to create the WinForm. Templates and starter kits are packages of files that you can use to quickly generate a new file or project. We will discuss how to generate these packages later in this chapter, but for now, let us examine the Content Installer XML necessary to install a template or starter kit.

A .vscontent file used to install a template file looks very much like the sample .vscontent file that we saw earlier. The <FileContentType> name for a template installer is VSTemplate. This content type installs files only with the .zip extension. Three different attributes are required to specify a content item for a template. The .vscontent XML schema allows attributes to be attached to a content item to provide custom data to the installer, directing the installer on how the item should be installed. Attributes use a name and value pair not only to name the data, but also to contain the data. The three attributes that a VSTemplate content item requires are TemplateType, ProjectType, and ProjectSubType. The value for a TemplateType attribute can be one of Project or ProjectItem, and it declares where the template can be used. If this value is Project, the template is copied into a location so that you can create the project through the New Project dialog box. If this value is ProjectItem, the file is copied into a location so that you can right-click in the Solution Explorer on a project or a folder within a project, choose New Item from the Add menu, and choose the template to add to an existing project. The second attribute necessary within a .vscontent file for a VSTemplate is the ProjectType attribute. This attribute specifies the programming language used for the template. Suppose you have a Microsoft Visual C#® project open; you would not want your Microsoft Visual Basic® template to appear within the Add New Item dialog box. And if you have the New Project dialog box open with the Visual Basic project type selected, you do not want your Microsoft Visual J#® project to appear. With the ProjectType attribute, you can create a filter that determines in which dialog box the template will appear. The possible values for this attribute are Visual C#, Visual Basic, Visual J#, and Web. (Although Web is not a programming language, it is a type of project.) The final attribute is the ProjectSubType attribute, which gives either the platform of the template if ProjectType is Visual C#, Visual J#, or Visual Basic; or the programming language of the project if ProjectType is Web. If the ProjectType is Web, the possible values for ProjectSubType are Visual C#, Visual Basic, or Visual J#. If the ProjectType is not Web, possible values for ProjectSubType are Windows, Smart Device, Database, or starter kits.

When the Content Installer copies the template file to disk it examines the values of these three attributes and uses them to construct a destination path where the template is placed. The installer begins with the Visual Studio user data directory, which is C:\Documents and Settings\username\My Documents\Visual Studio 2005, and then the installer appends Templates to this path because this is the storage location for all installed templates. Next the installer looks at the TemplateType attribute. If it is Project, it adds ProjectTemplates to the path, or, if the attribute value is ProjectItem, it adds ItemTemplates to the path. Next the value of ProjectType is added, and finally the ProjectSubType attribute value is added.

The following .vscontent file defines a Visual C# console application template named MyConsoleApplication.zip for Windows. Based on the values given in the XML, the .zip file for the template is installed into the location C:\Documents and Settings\username\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual C#\Windows.

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">  <Content>   <FileName>MyConsoleApplication.zip</FileName>   <DisplayName>My Console Application</DisplayName>   <FileContentType>VSTemplate</FileContentType>   <ContentVersion>1.0</ContentVersion>   <Attributes>     <Attribute name="ProjectType" value="Visual C#"/>     <Attribute name="ProjectSubType" value="Windows"/>     <Attribute name="TemplateType" value="Project"/>   </Attributes>  </Content> </VSContent> 

When the Content Installer has installed this .vscontent file, you will be able to create a console application within the new project dialog box. The template will appear under the node Visual C#\Windows in the tree on the left side of the dialog box and in the My Templates section of the list on the right side of the dialog box.

image from book
Wizards, Templates, and Starter Kits

When you create new projects or items for a project, you are likely to see the terms wizards, templates, and starter kits. Each is a different method for creating new code.

The most basic technique for creating code is through a wizard. A wizard is a COM object that you write from scratch to perform all the work necessary to generate a project or a project item. The template wizard, which consumes templates to generate projects or project items, is an implementation of a wizard. You do not need to write code—you need only to provide the sources for a project or project item and the template wizard handles re-creating the project or project item for you.

A starter kit is a template. A template .zip file and a starter kit .zip file physically are identical—the only difference is in the terminology. A starter kit is a project template that generally builds a more complex project. Visual Studio ships with a couple of starter kits, such as one you could use for a screen saver application or a Movie Collection tracking program. These projects obviously perform specific tasks, whereas a WinForm application simply serves as a starting point for your application.

image from book

Installing Controls to the Toolbox

Controls are DLLs that are inserted into the Toolbox, which the user can drag and drop onto a WinForm or ASPX Web page designer to quickly create the user interface for an application. These pre-built components save users a lot of time by allowing them to quickly design a program without needing to write lots of code. You can use the Content Installer to install controls in the Toolbox. To install a control, a DLL containing the control must be copied into a place where it can be found, and then Visual Studio must be started so that it can load and then add the control to the Toolbox.

Placing the control into a place where Visual Studio can find it is easy—at least with the Content Installer. When the Content Installer installs a control for the Toolbox, the control is placed into the directory My Documents\Visual Studio 2005\Controls\CompanyName, where CompanyName is the name of your company, group, or any other unique name that you want to use. When the Content Installer installs controls, it will use this CompanyName to create a new tab in the Toolbox (if one with that name does not already exist), and then the controls contained within the DLLs in this directory will be added to that Toolbox tab. This procedure allows you to create a tab in the Toolbox that distinguishes your controls from the default ones installed by Visual Studio or other companies.

Note 

You can also place controls into the directory My Documents\Visual Studio 2005\Controls. If you place a DLL here, the controls the DLL contain will be placed on a tab named My Controls.

The second step to install the control is to invoke Visual Studio, Visual Basic Express, Visual C# Express, and so on to allow those programs to install the control. Installing controls can be costly in terms of startup performance, so the control installer will start each of these applications to place the controls on the Toolbox with the command programname /Command Tools.InstallCommunityControls where programname is the full path to the executable for the various editions of Visual Studio. The command Tools. InstallCommunityControls invokes code within Visual Studio to search for, and then install the controls in the Toolbox. (We will more fully discuss commands in Chapter 7.) You can also use the command Tools.InstallCommunityControls outside of the control installer to reinitialize any controls you might have installed but that do not appear in the Toolbox. Suppose you install Visual C# Express, and later you decide to upgrade to Visual Studio; do you need to reinstall all the controls you might have downloaded? No. Simply start Visual Studio, open the Command Window tool window, type Tools. InstallCommunityControls, and then press Enter. Visual Studio will look for any controls that have previously been installed with the Content Installer and place them on the Toolbox.

When we were discussing the .vscontent file format, we saw a sample .vscontent file containing the information necessary to install a control. The name of the content type for a control is Toolbox Control, which should be placed within the <FileContentType> tag. In this example, a tab named CompanyName will be created within the Toolbox, and any controls in the file Control.dll will be added to that tab.

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">  <Content>   <FileName>CompanyName\Control.dll</FileName>   <DisplayName>Test control</DisplayName>   <Description>A control to test with</Description>   <FileContentType>Toolbox Control</FileContentType>   <ContentVersion>1.0</ContentVersion>  </Content> </VSContent> 

This also means that you need to create a folder named CompanyName in the folder containing the .vscontent file, and you would place the Control.dll file within the CompanyName folder.

Installing Code Snippets

Code Snippets are a source code editor productivity enhancer. If you are editing a C# file and you need to create, for example, a for loop, you could manually enter the text for that for loop, or you could use a snippet to create the loop for you. To use a snippet, first type the word for; this will cause the IntelliSense® window to appear. If you were to press the Tab key twice with the for keyword selected in the IntelliSense window, Visual Studio will automatically generate the basic structure of a for loop, as shown in Figure 4-4.

image from book
Figure 4-4: The basic structure of a for loop, as generated from a snippet

The control variable is named i, but you might not always want this variable to have that name. To change this variable name, just type in the new name. When you are finished typing, press the Tab key, and the variable name i, as well as all uses of the variable i within the snippet code, will be renamed, as shown in Figure 4-5, where the variable name is changed to j.

image from book
Figure 4-5: The basic for loop, with the control variable renamed to j

The variable name length is highlighted next, and it, too, can be edited. After typing the number 10 (creating a loop over the numbers 0 to 9, inclusive), the for loop appears like the one in Figure 4-6.

image from book
Figure 4-6: The basic for loop, with the upper limit set to 10

Although this is not a lot of text to type, imagine typing this bit of code over and over, just as you do during your development work, and you can see the amount of keystrokes you will save. Snippets are stored in an XML format and can be installed in the same manner as other content items.

A .snippet file can contain many different snippets, but each file can contain snippets for only one particular programming language. The Visual Basic, Visual C #, Visual J #, and XML editors currently support snippets. When installing a snippet, the <Content> tag must contain an <Attribute> tag indicating which programming language the snippet supports. The name of this attribute is lang, and the possible values are vb, csharp, jsharp, and xml. The following .vscontent file contains two items to install: a Visual Basic and a Visual C# snippet:

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">  <Content>   <FileName>VBSnippets.Snippet</FileName>   <DisplayName>Visual Basic snippets</DisplayName>   <Description>Visual Basic snippets to install.</Description>   <FileContentType>Snippet</FileContentType>   <ContentVersion>1.0</ContentVersion>   <Attributes>    <Attribute name="lang" value="vb"></Attribute>   </Attributes>  </Content> <Content>   <FileName>CSSnippet.Snippet</FileName>   <DisplayName>C# snippets</DisplayName>   <Description>C# snippets to install. </Description>   <FileContentType>Snippet</FileContentType>   <ContentVersion>1.0</ContentVersion>   <Attributes>    <Attribute name="lang" value="csharp"></Attribute>   </Attributes>  </Content> </VSContent> 

Installing Add-ins and Macros

In Chapters 5 and 6, we'll see how to create macros and add-ins, but while we are discussing installing files, we can show you how to install these types of files. An add-in (at least an add-in written using a .NET Framework programming language) consists of at least one file, and possibly more. The required file is a file with the .addin extension. This file describes the add-in to Visual Studio and provides information such as the name and description of the add-in. The second possible file is a .dll that contains the code for the add-in. The content type for an add-in is Addin, and this is one of the few content types that will accept more than one <FileName> tag to specify more than one file to install. This content type does not need any <Attribute> tags. Here is a simple .vscontent file used to install an add-in:

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">  <Content>   <FileName>MyAddin.Addin</FileName>   <FileName>MyAddin.dll</FileName>   <DisplayName>An add-in to install.</DisplayName>   <Description>This is a test add-in.</Description>   <FileContentType>Addin</FileContentType>   <ContentVersion>1.0</ContentVersion>  </Content> </VSContent> 

A macro, much like an add-in, is a file that contains program code that the user can run to customize and modify data within Visual Studio. The difference between a macro and an add-in is that the user can edit and modify the macro code, and there is an editing and debugging environment devoted to just this type of code. The XML used to install a macro has the <FileContentType> of Macro Project, and the only file types that can be installed by the macro installer are files that end with a .vsmacros extension:

 <VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">  <Content>   <FileName>Samples.vsmacros</FileName>   <DisplayName>A macro to install.</DisplayName>   <Description>A macro project to install.</Description>   <FileContentType>Macro Project</FileContentType>   <ContentVersion>1.0</ContentVersion>  </Content> </VSContent> 

Add-in and macro content installers are a bit different than the other installer types in that these content types will install only for Visual Studio. The Express versions of Visual Studio do not support macros or add-ins, so although templates, snippets, and controls can be installed for all those versions of Visual Studio, add-ins and macros will be available only from within the Enterprise, Pro, or other premium versions of Visual Studio.

Zipping

After you have created the content you want to distribute, the next step is to package the file for distribution. You do this by zipping up the files—you can use either the zip functionality built into Windows XP or any other zipping tool to do this—then rename the file to have the extension .vsi rather than .zip. If you are using Windows Explorer to zip the files, simply select the files to zip, right-click each file, and from the shortcut menu, choose Send To and then Compressed (zipped) Folder from the submenu. If you are using another zipping tool, such as WinZip, you need to make sure that you do not use some of the more advanced options these zip utilities provide. For example, you shouldn't use encryption or a nonstandard compression algorithm, and you shouldn't save files with full path information to zip your files. If you do, then extraction could fail within the Content Installer when the file is opened because the zip utility that the Content Installer uses supports only basic zipping functionality.

Note 

The extension .vsi has its roots in the file extension .msi, which stands for Microsoft installer. VSI is short for Visual Studio installer. The two technologies, however, should not be confused with one another. The Content Installer cannot be used to install .msi files, and the Microsoft installer cannot be used to install .vsi files.

After you have renamed the .zip file to have the extension .vsi, you can either e-mail or post the file to a Web site so that friends or colleagues can install the content that you have created. But before you make the .vsi file available to others for use, you will want to test the file to make sure it works. You could just double-click the .vsi file from within Windows Explorer, but if you find a bug in your content, you would need to correct the problem, then re-zip and rename the extension, and repeat. Rather than taking the time to go through the zip-rename process, you can just double-click the .vscontent file. Both the .vsi and .vscontent extensions are associated with the Content Installer. You can also use the Content Installer to browse to a .vsi, .zip, or .vscontent file. From the Run dialog box in the Windows Start menu, navigate to C:\Program Files\Common Files\Microsoft Shared\MSEnv and run the command vscontentinstaller.exe /browse. This will display the standard Windows Open File dialog box, where you can browse to any file you need to open and install the content the file contains.

Signing Your Content

Some people might be wary of installing content on their computers from unknown sources, but if content has been identified as coming from a trusted source, they might not be as concerned about installing that content. You can purchase Authenticode certificates from certificate authorities such as Thawte.com or VeriSign.com. But there is a problem: you cannot sign .zip files (or .zip files renamed to have the .vsi extension)—you can sign only .dll, .exe, or .cab files. To enable you to sign your .vsi files, Visual Studio has a utility named MakeZipExe, which will take a .zip file and create a self-extracting .exe that you can then sign by using the SignCode tool, a utility that is part of the .NET Frameworks software development kit (SDK). To create a .vsi file that you can sign, first generate your .zip file by using your favorite zip utility just as you would for unsigned content, but do not rename the file extension. Then, from the command line, run the following command:

 MakeZipExe -zipfile:"path to your .zip file" 

This command will create an .exe file in the same location as the zip with the same file name except for the extension. Next you will need an Authenticode certificate and the signcode.exe utility. Because the command line arguments to the signcode.exe tool can vary between the different certificate authorities, you should search the Web site of your certificate issuer for assistance in using the Signcode.exe tool. After you have signed the .exe file, you can rename the file to have the .vsi extension. When the Content Installer opens the .vsi file, it will examine the file to determine if the file is an unsigned .zip file or a signed .exe file and extract the content appropriately.

When signing code, you should keep two things in mind. First, certificates are not cheap. Certificates can cost from a few hundred dollars to thousands of dollars depending on the certificate, so you will need to decide if signing your code is worth the cost. If your target audience is consumers buying controls from your Web site, you probably will want to sign your .vsi. If you are only sharing your files with friends, you probably will not need to sign. The second thing about signing is that the Content Installer knows how to read self-extracting .exe files generated by using only the MakeZipExe tool; other tools, such as WinZip, do not produce .exe files compatible with the Content Installer.

image from book
The ContentBuilder Utility

Creating .vsi files is not overly complicated, but it can be made easier. The samples for this book include the sources to a tool named ContentBuilder. This tool provides a graphical user interface (GUI) for building a .vsi file just by selecting files on your computer's disk drive.

image from book




Working with Microsoft Visual Studio 2005
Working with Microsoft Visual Studio 2005
ISBN: 0735623155
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net