2009-09-07 47 views
1

我在物理路徑F:\ SAMPLEPRODUCT \ Bin中有一個exe文件simpleservice.exe,我需要獲取該exe文件的版本號,可以提供獲取版本號所需的代碼來自exe文件的信息

+0

是simpleservice.exe寫的什麼語言? –

+0

http://stackoverflow.com/questions/1111541/how-to-reference-both-assemblyversion-and-assemblyfileversion –

+0

或者如果你只是想在資源管理器中看到它?然後右鍵單擊並轉到屬性|細節。 –

回答

3

我會使用以下方法來做到這一點:

Assembly.LoadFrom("...").GetName().Version.ToString(); 

或我使用FileVersionInfo類。任你選:

FileVersionInfo.GetVersionInfo("..."); 
+0

這隻適用於.NET程序集,不適用於任何可執行程序 –

+0

我不小心打到了提交,而不是完成在我的評論的其餘部分輸入。我重新編輯它以顯示點擊提交時我錯過的位。 –

+0

這裏面Assembly.LoadFrom(「...」)我們需要給exe或物理路徑嗎? – peter

5

您可以使用

FileVersionInfo.GetVersionInfo

如:

public void GetFileVersion() { 
    // Get the file version for the exe. 
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo("your_exe_file"); 

    // Print the file name and version number. 
    textBox1.Text = "File: " + myFileVersionInfo.FileDescription + '\n' + 
     "Version number: " + myFileVersionInfo.FileVersion; 
} 
+0

+1雖然這不會工作假定程序集生成與fileversion和assemblyversion設置爲相同的東西? –

2
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(@"F:\SAMPLEPRODUCT\Bin\simpleservice.exe"); 
Console.WriteLine(fvi.FileVersion); 
+0

我把這些代碼在Form1_Load事件,但沒有顯示 – peter

+0

我知道了,這段代碼工作正常 – peter

+0

是否有任何其他方式來找到文件的位置意味着物理路徑,這裏的意思是'simpleservice.exe'是在這些物理路徑F:\ SAMPLEPRODUCT \ Bin 有沒有其他簡單的常見方法來查找ASP.NET中的server.mappath路徑,因爲如果我們將路徑複製到不同的文件 – peter

1
AssemblyName anm = AssemblyName.GetAssemblyName( 
    "c:\\winnt\\microsoft.net\\framework\\v1.0.3705\\mscorlib.dll"); 
    // and show its version 
    Console.WriteLine(anm.Version.ToString()); 
+0

這隻適用於.NET程序集,不適用於任何可執行文件 –

+0

是,但他用winform和C#標記標記問題,它給了我一個假設,他的意思是.net程序集 –

1
 

AssemblyName.GetAssemblyName(@"F:\SAMPLEPRODUCT\Bin\simpleservice.exe").Version 
 
+0

是否有任何其他方式來查找文件的位置意思是物理路徑,,這裏的意思是'simpleservice.exe'是在這些物理路徑中的:F:\ SAMPLEPRODUCT \ Bin 在ASP.NET中有沒有其他尋找像server.mappath這樣的路徑的簡單常見方法,因爲路徑會改變一些時間吶,如果我們將其複製到不同的 文件 – peter

+0

此代碼使用一行代碼,,更優化的 – peter

1
public string AssemblyVersion 
     { 
      get 
      { 
       return Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
      } 
     } 
相關問題