How to get installation path of an application?

后端 未结 6 1494
轻奢々
轻奢々 2020-12-06 05:54

In Windows using C#, how can I get the installation path of a software (for example consider NUnit or any other software like MS word, etc.) from my project

相关标签:
6条回答
  • 2020-12-06 06:12

    Steps to extract value from registry are shown in following code snippet. You may already know that there are no standard rules for applications to place their installation info. The steps shown below are for COM based applications where the appplication must provide Local executable path in a reasonably standard manner.

    For non-com applications, check to see if some data can be extracted from installed applications cache.

    I hate to admit that the solution is not as elegant as I want it to be. Each subkey has to opened in series and opening in single method does not work.

    //string hiveName = @"CLSID"; // for 64 bit COM 7applications
    string hiveName = @"WOW6432Node\CLSID"; // for 32 bit COM applications
    using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(hiveName))
    
    {
      if (key != null) {
          using (RegistryKey key2 = key.OpenSubKey("{<YourAppGUID>}"))
      {
      if (key2 != null) {
        using (RegistryKey key3 = key2.OpenSubKey("LocalServer32"))
      {
      if (key3 != null) {
        return key3.GetValue("").ToString();
      }
    }
    
    0 讨论(0)
  • 2020-12-06 06:17
    Application.ExecutablePath (includes filename)
    Application.StartupPath (not includes filename)
    

    This will give you the path where the application started. Hopefully it will be the installation path.

    0 讨论(0)
  • 2020-12-06 06:17
    string appFileName = Environment.GetCommandLineArgs()[0];
    

    will give you the full path of the executable and

    string directory = Path.GetDirectoryName(appFileName);
    

    extracts the directory.

    string envPath = Environment.GetEnvironmentVariable("PATH");
    Environment.SetEnvironmentVariable(envPath + ";" + yourPath); 
    

    edits the PATH environment variable for the current process.

    0 讨论(0)
  • 2020-12-06 06:31

    Use the system and application classes. This will give you all sorts of information.

    EG: Application.ExecutablePath

    It also provides methods to do what you want to.

    Edit: Also see registry read/write instructions here:

    http://www.c-sharpcorner.com/UploadFile/sushmita_kumari/RegistryKeys102082006061720AM/RegistryKeys1.aspx?ArticleID=0ce07333-c9ab-4a6a-bc5d-44ea2523e232

    0 讨论(0)
  • 2020-12-06 06:38

    Application.StartupPath is used to get installation location in c#.

    0 讨论(0)
  • 2020-12-06 06:39

    Like if i install Nunit in "C:\Program Files" i can run it by giving 'nunit' in cmd prompt but if i install in a different location i cant do the same.

    May be you are using Windows Vista, which can search in Program Files, but won't look in other folders.

    In windows using C#, how to get the installation path of a software(for example consider nunit).?

    It depends, how you are installing the application. The installer knows the path, you may program the installer to write that path to somewhere, say registry.

    Also how to set the path variables that we set in Environment variables so that we can run the application just by giving in command prompt.

    How do I get and set Environment variables in C#?

    0 讨论(0)
提交回复
热议问题