![](/img/trans.png)
[英]How to get assembly version of a project in a visual studio 2008 deployment project
[英]How to get the product Version number which is stored in the Deployment Project?
我有一個用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?";
你可以試試這個:
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();
}
如果只想在應用程序的“關於”框中顯示應用程序版本,則可以從static屬性獲取當前版本,如下所示:
My.Application.Info.Version
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.