how do I Locate an installed applications path the "proper" way from C#?

  • Thread starter Thread starter seanick
  • Start date Start date
S

seanick

I am trying to locate an installed application, and I so far have been
unable to do so programattically. There does not appear to be a way to do so
directly from the .NET framework, either 1.1 or 2.0 beta.
Therefore I have tried to use C# and the DllImport attribute to call
MsiGetProductInfo, however it doesnt seem to do anything. I might be using
it incorrectly, can someone check this and/or offer suggestions on something
else to try?
I don't want to just scan the local drives to find the file I am looking
for. call it a pride thing. or a not wanting to get fired for hack-like
code, which the below is already approaching.

/// Dllimport piece:
[DllImport("msi.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern uint MsiGetProductInfo(
[MarshalAs(UnmanagedType.LPTStr)] String szProduct,
[MarshalAs(UnmanagedType.LPTStr)] String szProperty,
StringBuilder lpValueBuf,
ref Int32 pcchValueBuf);

////the part that calls MsiGetProductInfo:
public string GetInstallLocation(string ProductCode)
{
Int32 iSize = 256;
StringBuilder sb = new StringBuilder(iSize);
try
{
LogMsg("sizeof iSize is " + iSize.ToString());
MsiGetProductInfo(ProductCode, "INSTALLPROPERTY_INSTALLLOCATION", sb, ref
iSize);
sb.EnsureCapacity(iSize);
LogMsg("sizeof iSize is " + iSize.ToString());
MsiGetProductInfo(ProductCode, "INSTALLPROPERTY_INSTALLLOCATION", sb, ref
iSize);
LogMsg("contents of sb is " + sb.ToString());
}
catch (Exception e)
{
DumpException(e);
}
return sb.ToString();
}

//// the output:
/// sizeof iSize is 256
/// sizeof iSize is 256
/// contents of sb is
 
I think there is a way, however you will still search your drives.
Albeit in a targetted way.

The registry key
MyComputer/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Installer/Folders
has as sub keys all of the folders where software has been installed.
Of course this assumes that your software was installed using the
Microsoft Windows installer.

Hope this helps...

Christian Gross
 
Back
Top