Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用windows api读取当前安装的应用程序版本_C#_Windows_Winapi - Fatal编程技术网

C# 使用windows api读取当前安装的应用程序版本

C# 使用windows api读取当前安装的应用程序版本,c#,windows,winapi,C#,Windows,Winapi,我试图使用windows api查找已安装应用程序的版本信息 我使用升级代码使用MsiEnumRelatedProducts api查找产品代码,但是当我尝试使用产品代码使用MsiGetProductInfo时,版本信息返回为垃圾 以下是我的MsiGetProductInfo api: [DllImport("msi.dll", CharSet = CharSet.Unicode)] private static extern Int32 MsiGetProductInfo( strin

我试图使用windows api查找已安装应用程序的版本信息

我使用升级代码使用MsiEnumRelatedProducts api查找产品代码,但是当我尝试使用产品代码使用MsiGetProductInfo时,版本信息返回为垃圾

以下是我的MsiGetProductInfo api:

[DllImport("msi.dll", CharSet = CharSet.Unicode)]
private static extern Int32 MsiGetProductInfo(
    string product, string property, [Out] StringBuilder valueBuf,
    ref Int32 len);

MsiGetProductInfo(sbProductCode, "INSTALLPROPERTY_INSTALLVERSION", builder, ref len);

关于我做错了什么有什么想法吗?

Application.ProductVersion适合我,无需手动调用WinAPI(尽管我仍在.Net 1.1中)

以下是我解决问题的方法

        Int32 m_len = 11512;
        StringBuilder m_versionInfo = new StringBuilder(m_len);

        StringBuilder m_sbProductCode = GetProductCodeFromMsiUpgradeCode();
        MsiGetProductInfo(m_sbProductCode.ToString(), "**VersionString**", m_versionInfo, ref m_len);

        return m_versionInfo.ToString();

这确实返回了版本字符串,并将其从十进制转换为字符串格式,如1.4.3。

响应@JoshHetland,要传递的字符串是
INSTALLPROPERTY\u VERSIONSTRING
的CamelCase后缀-请记住MSI区分大小写

因此:

INSTALLPROPERTY\u VERSIONSTRING
变为
VERSIONSTRING

INSTALLPROPERTY\u INSTALLDATE
变为
INSTALLDATE

等等


可用属性的完整列表在上。

@Erick:我刚刚更新了我的问题。我想这对当前的应用程序就可以了。一般来说,您可以从系统中使用FileVersionInfo。诊断:FileVersionInfo OfLeverionInfo=FileVersionInfo.GetVersionInfo(sSomeFileName);字符串sVersion=ofilervisioninfo.FileVersion@埃里克:在我看来,使用fileversion不是一个安全的想法,因为你假设文件将安装在那个位置。使用windows api将是一个更好的选择。我自己解决了这个问题:我只需将字符串属性更改为使用VersionString,这就解决了问题。您应该将代码作为答案发布。感谢您指出TMB,我将记住它,以备下次使用。您在哪里找到合适的字符串?我尝试了“INSTALLPROPERTY\u VERSIONSTRING”、“ProductVersion”、“INSTALLPROPERTY\u VERSION”以及通过MSDN找到的所有其他表示形式,“VERSIONSTRING”在任何地方都找不到。。。你在什么地方找到实际值的列表了吗?Tia非常感谢您指出本应记录在InstallShield或MSDN中的注释。