使用 C# exe 修改不同 C# exe 的资源

Ch3*_*33f 5 c# resources .net-assembly

解决了!见下文。

我有 2 个 C# 应用程序。应用程序 a 应该修改应用程序 b 的内部资源。应用程序 b 应该在执行时对其(修改后的)资源执行某些操作。

我怎样才能做到这一点?

这是我尝试过的:

public static void addFileToResources(string dest, string src)
{
    Assembly a_dest = Assembly.LoadFile(dest);

    using (Stream s_dest = a_dest.GetManifestResourceStream("Elevator.Properties.Resources.resources"))
    {
        using (ResourceWriter rw = new ResourceWriter(s_dest))
        {
            byte[] b_src = File.ReadAllBytes(src);
            rw.AddResource("target", b_src);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在EDIT上得到了一个System.ArgumentException, 因为 .net 资源似乎不可能:还有其他方法吗? 我想生成一个可执行的单个文件(即 application的 exe ),并且可以处理从 application 执行之前给出的数据(存储在 exe 中)。最好不必为了给它数据而实际编译。 为了使它更容易一些假设:The stream is readonly.System.Resources.ResourceWriter..ctor(Stream stream)



bab


  • a 总是在之前执行 b
  • a 只执行一次
  • 两个应用程序都是我写的

编辑 -
解决方案 由于无法通过资源实现这一点,我使用了以下解决方法:
显然,您可以将任何内容附加到 exe 文件中,它仍然可以执行,因此我想出了以下方法:

public class Packer : IDisposable
{
    // chosen quite arbitrarily; can be anything you'd like but should be reasonably unique
    private static byte[] MAGIC_NUMBER = { 0x44, 0x61, 0x6c, 0x65, 0x6b, 0x4c, 0x75, 0x63 };

    private Stream inStream;

    public Packer(string filename, bool openReadonly = false)
    {
        // The FileAccess.Read is necessary when I whant to read from the file that is being executed.
        // Hint: To get the path for the executing file I used:
        // System.Reflection.Assembly.GetExecutingAssembly().Location
        inStream = File.Open(filename, FileMode.Open, openReadonly ? FileAccess.Read : FileAccess.ReadWrite, openReadonly ? FileShare.Read : FileShare.None);
    }

    public byte[] ReadData(int index)
    {
        byte[] mn_buf = new byte[MAGIC_NUMBER.Length];
        byte[] len_buf = new byte[sizeof(Int32)];
        int data_len = 0;
        inStream.Seek(0, SeekOrigin.End);
        for (int i = 0; i <= index; ++i)
        {
            // Read the last few bytes
            inStream.Seek(-MAGIC_NUMBER.Length, SeekOrigin.Current);
            inStream.Read(mn_buf, 0, MAGIC_NUMBER.Length);
            inStream.Seek(-MAGIC_NUMBER.Length, SeekOrigin.Current);
            for (int j = 0; j < MAGIC_NUMBER.Length; ++j)
            {   // Check if the last bytes are equals to my MAGIC_NUMBER
                if (mn_buf[j] != MAGIC_NUMBER[j])
                {
                    throw new IndexOutOfRangeException("Not enough data.");
                }
            }
            inStream.Seek(-sizeof(Int32), SeekOrigin.Current);
            inStream.Read(len_buf, 0, sizeof(Int32));
            inStream.Seek(-sizeof(Int32), SeekOrigin.Current);
            // Read the length of the data
            data_len = BitConverter.ToInt32(len_buf, 0);
            inStream.Seek(-data_len, SeekOrigin.Current);
        }
        byte[] data = new byte[data_len];
        // Read the actual data and return it
        inStream.Read(data, 0, data_len);
        return data;
    }

    public void AddData(byte[] data)
    {
        // append it
        inStream.Seek(0, SeekOrigin.End);
        inStream.Write(data, 0, data.
        inStream.Write(BitConverter.GetBytes(data.Length), 0, sizeof(Int32));
        inStream.Write(MAGIC_NUMBER, 0, MAGIC_NUMBER.Length);
    }

    public void Dispose()
    {
        inStream.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想使用此代码段,请继续使用,但请注意,如果您将数据添加到文件中,检索时索引的顺序是相反的:
假设您先写入数据集 A,然后写入数据集 B,如果稍后读取数据,B 将有索引 0 和 A 索引 1。

use*_*830 4

根据您的假设,您可以使用Mono.Cecil库更新/添加可执行文件的资源

以下是使用 Mono.Cecil 进行资源操作的三种基本方法:

    public static void ReplaceResource(string path, string resourceName, byte[] resource)
    {
        var definition =
            AssemblyDefinition.ReadAssembly(path);

        for (var i = 0; i < definition.MainModule.Resources.Count; i++)
            if (definition.MainModule.Resources[i].Name == resourceName)
            {
                definition.MainModule.Resources.RemoveAt(i);
                break;
            }

        var er = new EmbeddedResource(resourceName, ManifestResourceAttributes.Public, resource);
        definition.MainModule.Resources.Add(er);
        definition.Write(path);
    }

    public static void AddResource(string path, string resourceName, byte[] resource)
    {
        var definition =
            AssemblyDefinition.ReadAssembly(path);

        var er = new EmbeddedResource(resourceName, ManifestResourceAttributes.Public, resource);
        definition.MainModule.Resources.Add(er);
        definition.Write(path);
    }

    public static MemoryStream GetResource(string path, string resourceName)
    {
        var definition =
            AssemblyDefinition.ReadAssembly(path);

        foreach (var resource in definition.MainModule.Resources)
            if (resource.Name == resourceName)
            {
                var embeddedResource =(EmbeddedResource) resource;
                var stream = embeddedResource.GetResourceStream();

                var bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);

                var memStream = new MemoryStream();
                memStream.Write(bytes,0,bytes.Length);
                memStream.Position = 0;
                return memStream;
            }

        return null;
    }
Run Code Online (Sandbox Code Playgroud)

您可以使用GetResource方法检索当前资源流(可写),

使用ResourceWriter,ResourceReaderResourceEditor类,您可以读/写或修改当前资源或创建新资源,然后只需通过调用将其放回可执行文件中ReplaceResource,或通过调用将其添加为新资源AddResource

以下是替换资源中图像的示例(通过从头开始创建新资源):

            var ms = new MemoryStream();
            var writer = new ResourceWriter(ms);
            writer.AddResource("good_luck",new Bitmap("good_luck.png"));
            writer.Generate();   
            ReplaceResource(@"my executale.exe", "ResourceTest.Properties.Resources.resources",ms.ToArray());
Run Code Online (Sandbox Code Playgroud)

你可以通过PM> Install-Package Mono.Cecilnuget获得塞西尔。