Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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# 如何获取存储在部署项目中的产品版本号?_C#_.net_Winforms_Deployment - Fatal编程技术网

C# 如何获取存储在部署项目中的产品版本号?

C# 如何获取存储在部署项目中的产品版本号?,c#,.net,winforms,deployment,C#,.net,Winforms,Deployment,我有一个用C#编写的WinForms应用程序。有一个创建setup.exe的部署项目,我在其中设置了版本号 如何在运行时获取此版本号,以便将其写入日志或显示在“关于”框中 我一直在使用下面的代码,但它不适用于64位安装 RegistryKey key = Registry.LocalMachine.OpenSubKey( @"Software\Microsoft\Windows\CurrentVersion\Uninstall"); string[] subKeyNames =

我有一个用C#编写的WinForms应用程序。有一个创建setup.exe的部署项目,我在其中设置了版本号

如何在运行时获取此版本号,以便将其写入日志或显示在“关于”框中

我一直在使用下面的代码,但它不适用于64位安装

RegistryKey key = Registry.LocalMachine.OpenSubKey(
        @"Software\Microsoft\Windows\CurrentVersion\Uninstall");
string[] subKeyNames = key.GetSubKeyNames();

foreach (string subKeyName in subKeyNames)
{
    Microsoft.Win32.RegistryKey subKey2 = key.OpenSubKey(subKeyName);

    if (ValueNameExists(subKey2.GetValueNames(), "DisplayName") 
        && ValueNameExists(subKey2.GetValueNames(), "DisplayVersion"))
    {
        string name = subKey2.GetValue("DisplayName").ToString();
        string version = subKey2.GetValue("DisplayVersion").ToString();
        if(name == "MyAppName") return version;
    }
    subKey2.Close();
}
key.Close();
return "v?";

如果您只想在应用程序的“关于”框中显示应用程序版本,可以从静态属性获取当前版本,如下所示:

My.Application.Info.Version
您可以尝试以下方法:

string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(registry_key))
{
    foreach (string subkey_name in key.GetSubKeyNames())
    {
        using (Microsoft.Win32.RegistryKey subkey = key.OpenSubKey(subkey_name))
        {
            if (!object.ReferenceEquals(subkey.GetValue("DisplayName"), null))
            {
                string[] str = subkey.GetValueNames();
                string SoftNames = Convert.ToString(subkey.GetValue("DisplayName"));
                if (SoftNames == "MyAppName")
                {
                    string Vendor_Publisher = Convert.ToString(subkey.GetValue("Publisher"));
                    string Version = Convert.ToString(subkey.GetValue("DisplayVersion"));
                    string InstallDate = FormatDateTime(subkey.GetValue("InstallDate"));
                }

            }
        }
    }
}



private static string FormatDateTime(object ObjInstallDate)
{
    object FinalDate = DBNull.Value;
    string strDate = Convert.ToString(ObjInstallDate);
    DateTime dtm;
    DateTime.TryParseExact(strDate, new string[] { "yyyyMMdd", "yyyy-MM-dd", "dd-MM-yyyy" }, 
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.None, out dtm);
    if (!String.IsNullOrEmpty(strDate))
    {                
        FinalDate = dtm;
    }
    return FinalDate.ToString();
}

这是C语言中的
System.Windows.Forms.Application.Info.Version
?如果是这样的话,那就行不通了找不到“信息”。适用于所有语言的信息是System.Reflection.Assembly.GetExecutionGassembly().GetName().Version