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

C# 如何编辑/更新exe文件的版本?,c#,.net,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

部分代码复制到此处:

    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);
    }
const Int32 RT_VERSION=16;
[DllImport(“kernel32.dll”,SetLastError=true)]
公共静态外部IntPtr BEGINUUPDATERESOURCE(字符串pFileName,
[Marshallas(UnmanagedType.Bool)]Bool-b删除现有资源;
[DllImport(“kernel32.dll”,SetLastError=true)]
公共静态外部布尔更新资源(IntPtr hUpdate、Int32 lpType、字符串lpName、ushort wLanguage、IntPtr lpData、uint cbData);
[DllImport(“kernel32.dll”,SetLastError=true)]
公共静态外部bool EndUpdateResource(IntPtr hUpdate,bool fDiscard);
[DllImport(“kernel32.dll”,SetLastError=true)]
静态外部IntPtr FindResource(IntPtr hModule,字符串lpName,Int32 lpType);
[DllImport(“kernel32.dll”,SetLastError=true)]
静态外部IntPtr加载资源(IntPtr hModule、IntPtr hResInfo);
公共枚举结果
{
成功,,
失败开始,
故障更新,
失效端
}
公共VRResult转换器(字符串exeFilePath,字符串a)
{
//加载可执行文件
IntPtr handleExe=beginupdatesource(exeFilePath,false);
if(handleExe==null)
返回VRResult.FailBegin;
//获取语言标识符
CultureInfo currentCulture=CultureInfo.currentCulture;
intpid=((ushort)currentCulture.LCID)和0x3ff;
intSID=((ushort)currentCulture.LCID)>>10;

ushort languageID=(ushort)((ushort)pid)您可以通过程序集文件更改Exe版本有2个STPE

第一个是通过汇编文件

 [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")]
第二种选择是通过项目属性


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

第一个是通过汇编文件

 [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")]
第二种选择是通过项目属性


但仅当您在更改.NET应用程序时意外地手头有源代码:)但仅当您在更改.NET应用程序时意外地手头有源代码:)