September 2nd, 2004, 12:11 AM
Getting installed app path in C#
Is there some simple way of determining:
1. If an app is installed
2. What the path to the app is
Im creating a patch utility which needs to find where the main app has been installed by the user
Any info would be appreciated
September 2nd, 2004, 03:29 AM
When you haven't done something before no matter how the solution is easy at first it seems very difficult anyway.
Add a Reference to the System.Management first. This namespace by default is not imported to your Windows Application. Project/Add Reference
Once it is added, add the following code to the top your form class
then use the following code to get all the installed applications and their properties
Please note; I added a Console.ReadLine() so that you can view the result in Output windows. Normally this window is placed on the bottom right corner of Visual Studio.
ManagementObjectSearcher MyWMIQuery = new ManagementObjectSearcher("SELECT * FROM Win32_Product") ;
ManagementObjectCollection MyWMIQueryCollection = MyWMIQuery.Get();
foreach(ManagementObject MyMO in MyWMIQueryCollection)
Console.WriteLine("Caption : " + MyMO["Caption"].ToString());
Console.WriteLine("Description : " + MyMO["Description"].ToString());
Console.WriteLine("InstallDate2 : " + MyMO["InstallDate2"].ToString());
//Some installed applications don't return installlocation
Console.WriteLine("InstallLocation : " + (MyMO["InstallLocation"] == null ? " " : MyMO["InstallLocation"].ToString()));
Console.WriteLine("InstallState : " + MyMO["InstallState"].ToString());
Console.WriteLine("Name : " + MyMO["Name"].ToString());
Console.WriteLine("PackageCache : " + MyMO["PackageCache"].ToString());
//Some installed applications don't return SKUNumber
Console.WriteLine("SKUNumber : " + (MyMO["SKUNumber"] == null ? " " : MyMO["SKUNumber"].ToString()));
Console.WriteLine("Vendor : " + MyMO["Vendor"].ToString());
Console.WriteLine("Version : " + MyMO["Version"].ToString());
MyWMIQueryCollection = null;
MyWMIQuery = null;
September 2nd, 2004, 04:29 AM
Thanks so much..I spent hours looking for an answer and trying different things.
I'll let you know how I get on....it's been a very long day
September 13th, 2004, 03:56 AM
sorry too take so long to get back.
I did try your solution and it did work..but - as you mentioned 'it may not return InstallLocation' - it didn't.
As I really needed the path, I decided to explore the registry options. It turned out that probing the registry was a lot easier than I had anticipated. In the process I've discovered why your solution may not always return the path (and a possible solution)
I used the Win32 api:
Then to locate the .Net Assemblies Installed (assuming it was an msi install):
The reason I use this registry 'address' is because MS keeps a record of all installed (msi) .Net assemblies. This means that if the installer for the .Net app doesn't add registry entries (the reason your routine doesn't locate them I'm guessing), they WILL be found here (as long as they are msi installed .NET apps).
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Installer\Assemblies");
Then traverse through all the installed .NET assemblies to locate the exe we are looking for:
if (key!=null) // Make sure there are Assemblies
foreach(string Keyname in key.GetSubKeyNames())
The registry entries are formatted with | (instead of \)
regfilepath = c:|path|YOUR_EXE_FILE.EXE
We replace the | with a \
'fullpath' will be the full path (including exe filename) to the app.
for (int a=0;a<regfilepath.Length;a++)
ie...fullpath = c:\path\YOUR_EXE_FILE.EXE
Now chop the filename off to obtain ONLY the path...
fullpath = c:\path\YOUR_EXE_FILE.EXE
path = c:\path\
I'm sure there is prolly better code for all this..but this code has achieved what I needed.
Now you can use 'fullpath' to access your exe and 'path' to locate the path to your exe
This is my first code submission..so sorry if I'm a bit sloppy.
Hope this will help someone else stuck where I was.
September 13th, 2004, 04:40 AM
I'm glad you have found a solution that worked for you and thank you for letting us to know about your solution. It has been appreciated
September 13th, 2004, 04:58 AM
I'll post some better code when I have a spare minute