解决!见下文。使用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
与The stream is readonly.
上System.Resources.ResourceWriter..ctor(Stream stream)
编辑
因为这似乎并不可能与.NET资源:有没有另一种方式?
我想生成一个单个文件(即应用程序b
的exe文件),它是可执行的,并且可以处理从应用程序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.
您是否验证过您将“非空”传递给'ResourceWriter'的构造函数? – 2014-09-30 14:44:33
两个问题。您没有正确猜测资源流名称,这是导致异常的原因。而当你解决这个问题时,你会遇到更严重的问题,资源嵌入到.NET程序集后是只读的。重建可执行文件是不实际的。 – 2014-09-30 14:44:36
呃...该死的。不管怎么说,还是要谢谢你! – Ch33f 2014-09-30 14:49:18