tags:

views:

1253

answers:

5

In windows using C#, how to 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 cant 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

+4  A: 

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

mm2010
u mean i can use this to get the path of any installed programs from my application.?
pragadheesh
See my edit (link) above.
mm2010
A: 

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.

http://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c/185214

Priyank Bolia
thanks for that link. but i need to get the installation folder of any application such as MS word,firefox etc.
pragadheesh
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
+1  A: 
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
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.
pragadheesh
I either totally misread your question or you changed it after my answer..
VVS
A: 

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

kedar kamthe
+3  A: 
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.