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



The information in this article applies to:

  • Microsoft Windows Installer
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

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

  1. Create a file that is named Readme.txt. To do this, follow these steps:
    1. Start a text editor (such as Notepad).
    2. 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.

    3. On the File menu, click Save.
    4. In the Save As dialog box, locate the folder where you created the MyApp project in the "Create a Setup Project" section of this article.
    5. In the File name text box, type Readme.txt.
    6. Click Save.
  2. Switch to Visual Studio .NET.
  3. On the Project menu, point to Add, and then click File.
  4. In the Add Files dialog box, locate and then click the Readme.txt file that you created in step 1.
  5. Click Open.

    The Readme.txt file is added to the MyApp project.
back to the top

Retrieve a Value from the Registry

The MediaPath value for your computer is located under the following registry subkey:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion

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.
  4. On the View menu, click Properties Window to select the Properties window for the registry search that you created in step 3.
  5. Change the Name property value to Search for MediaPath.
  6. 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.
  7. To search HKEY_LOCAL_MACHINE in the registry, click the Root property, and then click vsdrrHKLM in the drop-down list box for the property value.
  8. To search the SOFTWARE\Microsoft\Windows\CurrentVersion subkey, type SOFTWARE\Microsoft\Windows\CurrentVersion in the RegKey property value text box.
  9. 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 "Create a Setup Project" section of this article.
  4. Run the Setup.exe file.
  5. On the Welcome to the MyApp Setup Wizard page, click Next.
  6. 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.
  7. On the Confirm Installation page, click Next to install MyApp.
  8. On the Installation Complete page, click Close to quit your installation.
  9. 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.
  10. Open the Readme.txt file.

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

Modification Type:MinorLast Reviewed:5/23/2005
Keywords:kbRegistry kbSysSettings kbAppSetup kbPDWizard kbIDEProject kbDeployment kbsetup kbProgramming kbHOWTOmaster KB827026 kbAudDeveloper