467,915 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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

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
Nov 17 '05 #1
  • viewed: 4825
Share:
1 Reply
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

seanick wrote:
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

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by DKode | last post: by
1 post views Thread by Rick Brown | last post: by
7 posts views Thread by mwt | last post: by
7 posts views Thread by Erik Funkenbusch | last post: by
15 posts views Thread by Richard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.