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中開發的。
什麼問題? – Ian