Código
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using Microsoft.Win32; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr BeginUpdateResource(string pFileName, bool bDeleteExistingResources); [DllImport("kernel32.dll", SetLastError = true)] static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage, IntPtr lpData, uint cbData); [DllImport("kernel32.dll", SetLastError = true)] static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard); static unsafe void Main() { string path = "c:\\users\\sean\\desktop\\resourcer.exe"; IntPtr hResource = BeginUpdateResource(path, false); if (hResource.ToInt32() == 0) string newMessage = File.ReadAllText("c:\\users\\sean\\desktop\\newMessage.txt"); GCHandle bHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned); IntPtr ptr = bHandle.AddrOfPinnedObject(); ushort id = (ushort)Language.MakeLanguageID(); if (UpdateResource(hResource, "FILE", "eva.txt", id, ptr, (uint)bytes.Length)) EndUpdateResource(hResource, false); else MessageBox.Show("Did not update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public static class Language { public static int MakeLanguageID() { CultureInfo currentCulture = CultureInfo.CurrentCulture; int pid = GetPid(currentCulture.LCID); int sid = GetSid(currentCulture.LCID); return (((ushort)pid) << 10) | ((ushort)sid); } public static int GetPid(int lcid) { return ((ushort)lcid) & 0x3ff; } public static int GetSid(int lcid) { return ((ushort)lcid) >> 10; } } }
Para ver si funciona lo que hago es "descomprimir" el recurso del exe modificado.
Supuestamente deberia mostrar el nuevo .txt, pero no... cuando lo abro contiene el texto viejo.
Lo que hago para descomprimir los recursos del exe modificado es:
Código
string strNewPathToSave = "new.txt"; Assembly assembly = Assembly.GetExecutingAssembly(); Stream resourceStream = assembly.GetManifestResourceStream("WindowsFormsApplication2.Resources.eva.txt"); System.IO.FileStream fs = System.IO.File.OpenWrite(strNewPathToSave); try { // Save the File... resourceStream.Read(buffer, 0, (int)resourceStream.Length); fs.Write(buffer, 0, buffer.Length); } finally { fs.Close(); }
Redondeando....
Con exe1 cambio un recurso de exe2 (un .txt). Despues abro el exe2 y descomprimo el recurso .txt . Este en lugar de mostrar el nuevo texto, muestra el texto del recurso original.
Alguna idea?
Saludos