14

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? 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.

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 can't do the same. I need to get the location or path of NUnit or any other software installed in my system (having Windows XP) from my project.

EDIT: Like I can get the path of installed program from registry. HKEY_CURRENT_USER->SOFTWARE

JYelton
  • 35,664
  • 27
  • 132
  • 191
SyncMaster
  • 9,754
  • 34
  • 94
  • 137

6 Answers6

14

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.

Pablo Sarturi
  • 161
  • 1
  • 10
Johan Bresler
  • 6,450
  • 11
  • 56
  • 77
11
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.

9
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.

VVS
  • 19,405
  • 5
  • 46
  • 65
  • 1
    this give the location of the exe of my project, but i need to get the installation folder of any application such as MS word,firefox etc. – SyncMaster May 26 '09 at 09:56
2

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

kedar kamthe
  • 8,048
  • 10
  • 34
  • 46
0

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();
  }
}
Don Brody
  • 1,689
  • 2
  • 18
  • 30
CDJ
  • 1
0

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#?

Community
  • 1
  • 1
Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82
  • thanks for that link. but i need to get the installation folder of any application such as MS word,firefox etc. – SyncMaster May 26 '09 at 09:55
  • The only way i can think of is to read the environment path variable. Split the path, and search in all folders. Should not be a task. Some pseudo code: envPath = System.Environment.GetEnvironmentVariable ('Path'); string [] paths = envPath.Split(':'); foreach(string path in paths) File.Exists(Path.Combine(path, filename)); – Priyank Bolia May 26 '09 at 10:18