使用C#exe修改其他C#exe的资源
作者:互联网
解决了!见下文.
我有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);
}
}
}
我得到一个System.ArgumentException与流是只读的.在System.Resources.ResourceWriter..ctor(Stream stream)上
编辑
由于.net资源似乎无法实现:还有其他方法吗?
我想生成一个可执行文件,并且可以处理从应用程序a执行之前给出的数据(存储在exe中),该文件可以执行.最好不必实际编译b即可为其提供数据.
为了使其变得简单一些的假设:
> 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();
}
}
如果要使用此代码段,请继续,但是请注意,如果将数据添加到文件中,则检索时索引的顺序相反:
假设您先写入数据集A,然后再写入数据集B,如果稍后再读取数据,则B将具有索引0,而A将具有索引1.
解决方法:
根据您的假设,您可以使用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;
}
您可以使用GetResource方法检索当前资源流(可写),
使用ResourceWriter,ResourceReader或ResourceEditor类,您可以读取/写入或修改当前资源或创建新资源,然后只需通过调用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());
您可以通过PM>获取Cecil安装包Mono.Cecil nuget.
标签:net-assembly,c,resources 来源: https://codeday.me/bug/20191121/2050022.html