2012-07-18 18 views
1

可能重複:
How can I display the Build number and/or DateTime of last build in my app?如何獲取我的應用程序的最後寫入或構建時間?

我想,當我的應用程序的建立是爲了在標題欄的最後日期時間追加。但這個代碼:

DateTime lastWriteTime = File.GetLastWriteTime(Assembly.GetExecutingAssembly().GetName().ToString()); 
this.Text = String.Format("Platypi R Us; Built on {0}", lastWriteTime); 

...索賠是在400多年前(在1600年);我覺得不太可能。

+0

你剛纔不是問這個問題?http://stackoverflow.com/questions/11532705/how-can-i-display-the-build-number-and-or-date-of-last-build-in-my-app – zeroef 2012-07-18 16:17:25

+0

@zeroef:不,但是類似;我放棄了在包括版本信息,因爲這只是像「1.0.0.0」和內部編號不可用。所以我現在正在建立一個「最後建成時間」值,這是工作。 – 2012-07-18 18:04:28

回答

2

問題是你已經使用不是的文件名叫GetLastWriteTime。打印出Assembly.GetExecutingAssembly().GetName().ToString(),你會明白我的意思。

documentation for File.GetLastWriteTime調用了這一點,特別是:

如果path參數描述的文件不存在,這個方法返回午夜12點,1月1日,公元1601(CE)協調世界時(UTC ),調整到當地時間。

所以,要麼使用Application.ExecutablePath按照克萊的建議,或特定組件(或避免的WinForms依賴),你可以使用Application.ManifestModule並得到FullyQualifiedName,就像這樣:

using System; 
using System.Reflection; 

class Test 
{ 
    static void Main() 
    { 
     string file = typeof(Test).Assembly 
            .ManifestModule 
            .FullyQualifiedName; 
     Console.WriteLine(file); 
     DateTime lastWriteTime = File.GetLastWriteTime(file); 
     Console.WriteLine(lastWriteTime); 
    } 
} 

中當然,它只獲得包含程序集清單的模塊的最後寫入時間 - 可能有多模塊程序集。這很少見,而且我的猜測是,這會讓你很好。

這是一個恥辱,有的嵌入Assembly自身構建的時間概念,但生活就是這樣:(

+0

有趣的是,文檔獲取日期不對,它不重要(它顯示1600,而不是1601)。 – 2012-07-18 16:22:38

1

這工作:

FileInfo fileInfo = new FileInfo(Application.ExecutablePath); 
DateTime lastWriteTime = fileInfo.LastWriteTime; 
this.Text = String.Format("Platypi R Us; Built on {0}", lastWriteTime); 
1

'的GetName()' 返回組件的顯示名稱。你想Assembly.Location

此外,我不知道這會工作。如果將文件複製到其他位置,則文件時間與實際的最後編譯時間不同步。也許每次發佈新版本時都要手動更新它?

相關問題