19

I am deploying my application using an msi installer designed with a Visual Studio Setup Project. How do I set a registry key to the application's install path?

hlovdal
  • 26,565
  • 10
  • 94
  • 165
kjv
  • 11,047
  • 34
  • 101
  • 140

4 Answers4

30

Actually, while I was searching for the same thing the following solution was also mentioned:

use [TARGETDIR] in the registry key.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Cohen
  • 2,720
  • 26
  • 24
  • 1
    I used this method with a property of my own definition that gets passed into the msiexec.exe through the command line and it worked great. – skeletank Nov 03 '10 at 20:38
  • 2
    Thanks. This is a good, easy and straightforward way to solve the problem. Old post, still very much useful. Not sure why this is not checked as the answer. – kmarks2 Mar 29 '12 at 19:26
  • 7
    The goal of SE sites is to become a resource of knowledge, of answers, for years to come. With a link-only answer, the op must dig through another resource to locate an answer he/she might not be sure about. Most importantly, if your link were to ever break, (which it has) your answer is useless for anyone who visits this page in the future. I have given what I believe is the same answere here http://stackoverflow.com/a/11490003/495455 – Jeremy Thompson Jul 17 '12 at 01:41
  • 1
    Quick note that [TARGETDIR] already contains a trailing backslash so correct usage is [TARGETDIR]myapp.exe – Geordie Apr 05 '22 at 20:10
5

Just to add to putting [TARGETDIR] in the registry key as the value. If you are using the install shield for vs2012 use [INSTALLDIR] instead in the registry key.

user2117229
  • 135
  • 3
  • 8
4

One way to do this would be to create a custom action in your installer. On the custom action you could provide CustomActionData "/Path="[TARGETDIR]*". Within your custom action code you can reference Context.Parameters["Path"] and receive the installation path passed from the installer in your .NET code.

Now that you have the [TARGETDIR] within your custom action code you can continue to use the Microsoft.Win32 namespace to set the registry key.

HTH - Wil

Wil P
  • 3,341
  • 1
  • 20
  • 20
  • can you answer this question?http://stackoverflow.com/questions/5217478/how-can-get-the-path-from-installer-and-how-set-in-my-app – Farna Mar 11 '11 at 10:55
4
  1. follow this steps :
  2. Add a class library project into setup solution.
  3. Add installer file into your class library project.
  4. Add created class library project to your setup application folder
  5. Add created project installer file (On setup custom action window) to "Install" sub tree item.

enter image description here

  1. click on added project and press F4 to open Property window.
  2. on property window set "/pDir="[TARGETDIR]\" into CustomActionData.

enter image description here

  1. on installer file (in class library project) write the follow code to write install path into registry.

     Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        MyBase.Install(stateSaver)
        Dim regsrv As New RegistrationServices
        regsrv.RegisterAssembly(MyBase.GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase)
        '--------- adding installation directory to stateSaver ----------
        stateSaver.Add("myTargetDir", Context.Parameters("pDir").ToString)
    End Sub
    
    Public Overrides Sub Commit(ByVal savedState As System.Collections.IDictionary)
        MyBase.Commit(savedState)
        ''messagebox.show("salam")
        Dim InstallAddress As String = savedState("myTargetDir").ToString
        Dim regKey As RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software\pourab\Sanjande", True)
        regKey.SetValue("InstalledFolder", InstallAddress)
mehdi
  • 645
  • 1
  • 9
  • 9