如何编辑/更新exe文件的版本?

时间:2015-09-25 11:43:47

标签: c# .net

部分代码复制到此处:

    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 个答案:

答案 0 :(得分:0)

您可以通过程序集文件更改Exe版本,有2个stpes

第一个是通过汇编文件

 [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")]

第二个选项是通过项目属性 Go To Project Property and Click on Button which is Highlighted

Change Application Details