Microsoft KB Archive/827026

= HOW TO: Retrieve a Value from the Registry and Then Use the Value in a Setup Project in Visual Studio .NET =

Article ID: 827026

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft Windows Installer 2.0
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Setup Project
 * Add a File to Your Setup Project
 * Retrieve a Value from the Registry
 * Use the Retrieved Value to Change Your Installation Path
 * Verify That Your Application Works
 * REFERENCES



SUMMARY
This step-by-step article describes how to retrieve a value from the registry. This article also describes how to use this value in a setup project that you create by using Visual Studio .NET.

The examples in this article describe how to retrieve the MediaPath value for your computer from the registry, and then install your application to a folder that is located in the folder that your MediaPath value specifies.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topic:
 * Visual Studio .NET Setup Projects

back to the top

Create a Setup Project

 * 1) Start Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Setup and Deployment Projects.
 * 4) Under Templates, click Setup Project.
 * 5) In the Name text box, type MyApp.
 * 6) Click OK.

back to the top

Add a File to Your Setup Project
 Create a file that is named Readme.txt. To do this, follow these steps:  Start a text editor (such as Notepad). Type the following text in the text editor:

This application is installed in a folder that depends on a value that is read from the system registry.

 On the File menu, click Save. In the Save As dialog box, locate the folder where you created the MyApp project in the &quot;Create a Setup Project&quot; section of this article. In the File name text box, type Readme.txt . Click Save.</li></ol> </li> Switch to Visual Studio .NET.</li> On the Project menu, point to Add, and then click File.</li> In the Add Files dialog box, locate and then click the Readme.txt file that you created in step 1.</li> Click Open.

The Readme.txt file is added to the MyApp project.</li></ol>

back to the top

Retrieve a Value from the Registry
The MediaPath value for your computer is located under the following registry subkey:

You can retrieve this value by using a launch condition. To do this, follow these steps:
 * 1) In Visual Studio .NET, point to Editor on the View menu, and then click Launch Conditions.
 * 2) In the Launch Conditions Editor, click Search Target Machine.
 * 3) On the Action menu, click Add Registry Search.

By default, Search for RegistryEntry1 is added.
 * 1) On the View menu, click Properties Window to select the Properties window for the registry search that you created in step 3.
 * 2) Change the Name property value to Search for MediaPath.
 * 3) Change the Property property value to MEDIA_PATH.

When you run the setup project, the MediaPath registry value is retrieved to your MEDIA_PATH property.
 * 1) To search   in the registry, click the Root property, and then click vsdrrHKLM in the drop-down list box for the property value.
 * 2) To search the   subkey, type SOFTWARE\Microsoft\Windows\CurrentVersion in the RegKey property value text box.
 * 3) To search for the MediaPath value, type MediaPath in the Value property value text box.

back to the top

Use the Retrieved Value to Change Your Installation Path
To change your installation path based on the value of your MediaPath property, change the value of the DefaultLocation property for your application folder. To do this, follow these steps:
 * 1) Switch to the File System Editor.
 * 2) Under File System on Target Machine, click Application Folder.
 * 3) On the View menu, click Properties Window to select the Properties window for Application Folder.
 * 4) To change your installation path based on the value of your MediaPath property, change the DefaultLocation property value to [MEDIA_PATH]\MyApp.

When you run the setup project, this value causes Setup to install your application to the MyApp folder in your Media folder. For example, if your MediaPath value is C:\WINDOWS\Media, Setup installs MyApp to the C:\WINDOWS\Media\MyApp folder.

Note Notice that MEDIA_PATH is enclosed in square brackets in the property value. When you reference a property in any property other than a Condition property, enclose your original property in square brackets.

back to the top

Verify That Your Application Works

 * 1) On the Build menu, click Build Solution.
 * 2) Start Windows Explorer.
 * 3) Locate the Setup.exe file that you built in step 1.

Setup.exe is located in a folder named Debug. The Debug folder is located in the MyApp folder where you created your MyApp setup project in the &quot;Create a Setup Project&quot; section of this article.
 * 1) Run the Setup.exe file.
 * 2) On the Welcome to the MyApp Setup Wizard page, click Next.
 * 3) On the Select Installation Folder page, notice that the Folder text box contains your customized installation path. For example, if your MediaPath value is C:\WINDOWS\Media, the Folder text box contains C:\WINDOWS\Media\MyApp\. Click Next.
 * 4) On the Confirm Installation page, click Next to install MyApp.
 * 5) On the Installation Complete page, click Close to quit your installation.
 * 6) In Windows Explorer, locate the folder where you installed MyApp.

Note The path of this folder appeared in the Folder text box in step 6.

Notice that the Readme.txt file is located in this folder.
 * 1) Open the Readme.txt file.

Notice that the Readme.txt file contains the text that you typed in step 1b of the &quot;Add a File to Your Setup Project&quot; section of this article.

back to the top

<div class="references_section">