#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    43
    Rep Power
    12

    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

    Brad
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Posts
    929
    Rep Power
    11
    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

    Code:
    using System.Management;
    then use the following code to get all the installed applications and their properties

    Code:
    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());
    	Console.ReadLine();
    }
    MyWMIQueryCollection = null;
    MyWMIQuery = null;
    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.


    Good Luck
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    43
    Rep Power
    12
    WOOHOO!!!

    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

    Thanks
    Brad
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    43
    Rep Power
    12
    Hey there,
    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:
    Code:
    using Microsoft.Win32;
    Then to locate the .Net Assemblies Installed (assuming it was an msi install):
    Code:
    RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Installer\Assemblies");
    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).

    Then traverse through all the installed .NET assemblies to locate the exe we are looking for:
    Code:
    string regfilepath="";
    if (key!=null)       // Make sure there are Assemblies
    {
        foreach(string Keyname in key.GetSubKeyNames())
        {
            if (Keyname.IndexOf("YOUR_EXE_FILE.EXE")>0)
            {
    	regfilepath=Keyname;
    	break;
            }
       }
    }

    The registry entries are formatted with | (instead of \)
    ie...
    regfilepath = c:|path|YOUR_EXE_FILE.EXE

    So...
    We replace the | with a \

    Code:
    string fullpath="";
    for (int a=0;a<regfilepath.Length;a++)
    {
      if (regfilepath.IndexOf("|",a,1)>0)
       fullpath+="\\";
     else
      fullpath+=regfilepath.Substring(a,1);
    }
    'fullpath' will be the full path (including exe filename) to the app.
    ie...fullpath = c:\path\YOUR_EXE_FILE.EXE

    Now chop the filename off to obtain ONLY the path...
    Code:
    path=fullpath.Substring(0,fullpath.LastIndexOf("\\")+1);
    Now:
    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.

    Cheers
    Brad
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Posts
    929
    Rep Power
    11
    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

    Good Luck
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    43
    Rep Power
    12
    no problemo
    I'll post some better code when I have a spare minute

    Cheers
    Brad

IMN logo majestic logo threadwatch logo seochat tools logo