2015-09-25 122 views
0

部分被複制在這裏:如何編輯/更新exe文件的版本?代碼

const Int32 RT_VERSION = 16; 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern IntPtr BeginUpdateResource(string pFileName, 
    [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern bool UpdateResource(IntPtr hUpdate, Int32 lpType, string lpName, ushort wLanguage, IntPtr lpData, uint cbData); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern IntPtr FindResource(IntPtr hModule, string lpName, Int32 lpType); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo); 




public enum VRResult 
{ 
    Success, 
    FailBegin, 
    FailUpdate, 
    FailEnd 
} 

public VRResult ChangeVer(string exeFilePath , string a) 
{ 
    // Load executable 
    IntPtr handleExe = BeginUpdateResource(exeFilePath, false); 

    if (handleExe == null) 
     return VRResult.FailBegin; 

    // Get language identifier 
    CultureInfo currentCulture = CultureInfo.CurrentCulture; 
    int pid = ((ushort)currentCulture.LCID) & 0x3ff; 
    int sid = ((ushort)currentCulture.LCID) >> 10; 
    ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid)); 

    // Get pointer to data 
    GCHandle vers = GCHandle.Alloc(a, GCHandleType.Pinned); 
    IntPtr hRes = FindResource(handleExe, "#1", RT_VERSION); 
    IntPtr hGlobal = LoadResource(handleExe, hRes); 

    // Replace the EXE 
    // UpdateResource(handleExe, Convert.ToString("RT_VERSION"), Convert.ToString(" MAKEINTRESOURCE(VS_VERSION_INFO)"), languageID, handleExe, 0); 
    if (UpdateResource(handleExe, RT_VERSION, "#1", languageID, hGlobal, (uint)a.Length)) 
    { 
     if (EndUpdateResource(handleExe, false)) 
     { 
      MessageBox.Show("File Updated"); 
      return VRResult.Success; 
     } 
     else 
     { 
      MessageBox.Show("File Update Fail"); 
      return VRResult.FailEnd; 
     } 
    } 
    else 
    { 
     MessageBox.Show("File Update Terminated"); 
     return VRResult.FailUpdate; 
    } 
} 


    private void btnprocess_Click(object sender, EventArgs e) 
    { 
     string filePath = @"C:\Users\User\Downloads\Setup\A.exe"; 
     string a = "2.2.2.2"; 
     ChangeVer(filePath, a); 
    } 

我想更新任何exe文件的資源文件,並想改變它的版本(文件版本和產品版本)。

此代碼是在c#.net中開發的。

+1

什麼問題? – Ian

回答

0

您可以通過大會文件修改EXE文件版本有2個STPES

1一種是通過大會文件

[assembly: AssemblyTitle("TApplciation Name")] 
    [assembly: AssemblyDescription("")] 
    [assembly: AssemblyConfiguration("")] 
    [assembly: AssemblyCompany("")] 
    [assembly: AssemblyProduct("")] 
    [assembly: AssemblyCopyright("Copyright © 2015")] 
    [assembly: AssemblyTrademark("")] 
    [assembly: AssemblyCulture("")] 
    [assembly: AssemblyVersion("5.8.3")] 
    [assembly: AssemblyFileVersion("5.8.3")] 

和第2次選擇是通過項目物業 Go To Project Property and Click on Button which is Highlighted

Change Application Details

+1

但是,只有當你改變一個.NET應用程序,並且你意外地有源代碼:) – Gene

相關問題