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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seanick

    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 programatticall y. 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
    MsiGetProductIn fo, 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=tr ue, CharSet=CharSet .Auto)]
    public static extern uint MsiGetProductIn fo(
    [MarshalAs(Unman agedType.LPTStr )] String szProduct,
    [MarshalAs(Unman agedType.LPTStr )] String szProperty,
    StringBuilder lpValueBuf,
    ref Int32 pcchValueBuf);

    ////the part that calls MsiGetProductIn fo:
    public string GetInstallLocat ion(string ProductCode)
    {
    Int32 iSize = 256;
    StringBuilder sb = new StringBuilder(i Size);
    try
    {
    LogMsg("sizeof iSize is " + iSize.ToString( ));
    MsiGetProductIn fo(ProductCode, "INSTALLPROPERT Y_INSTALLLOCATI ON", sb, ref
    iSize);
    sb.EnsureCapaci ty(iSize);
    LogMsg("sizeof iSize is " + iSize.ToString( ));
    MsiGetProductIn fo(ProductCode, "INSTALLPROPERT Y_INSTALLLOCATI ON", sb, ref
    iSize);
    LogMsg("content s 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


  • Christian Gross

    #2
    Re: how do I Locate an installed applications path the "proper&qu ot; wayfrom C#?

    I think there is a way, however you will still search your drives.
    Albeit in a targetted way.

    The registry key
    MyComputer/HKEY_LOCAL_MACH INE/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:[color=blue]
    > I am trying to locate an installed application, and I so far have been
    > unable to do so programatticall y. 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
    > MsiGetProductIn fo, 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=tr ue, CharSet=CharSet .Auto)]
    > public static extern uint MsiGetProductIn fo(
    > [MarshalAs(Unman agedType.LPTStr )] String szProduct,
    > [MarshalAs(Unman agedType.LPTStr )] String szProperty,
    > StringBuilder lpValueBuf,
    > ref Int32 pcchValueBuf);
    >
    > ////the part that calls MsiGetProductIn fo:
    > public string GetInstallLocat ion(string ProductCode)
    > {
    > Int32 iSize = 256;
    > StringBuilder sb = new StringBuilder(i Size);
    > try
    > {
    > LogMsg("sizeof iSize is " + iSize.ToString( ));
    > MsiGetProductIn fo(ProductCode, "INSTALLPROPERT Y_INSTALLLOCATI ON", sb, ref
    > iSize);
    > sb.EnsureCapaci ty(iSize);
    > LogMsg("sizeof iSize is " + iSize.ToString( ));
    > MsiGetProductIn fo(ProductCode, "INSTALLPROPERT Y_INSTALLLOCATI ON", sb, ref
    > iSize);
    > LogMsg("content s 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
    >
    >[/color]

    Comment

    Working...