我有一个已编译的 .NET 程序集,其中嵌入了一个特定的资源文件(名为Script.xml").我需要以编程方式将其更改为另一个.
I have a compiled .NET assembly with a specific resource file embedded (named 'Script.xml'). I need to programmatically change it out for another.
不从源代码重新编译是否可以做到这一点?
Is this possible to do without recompiling from source?
目前,我搜索我知道文件中的文本,它运行良好.但是我需要为另一个我不知道资源文件的任何内容的项目这样做,我需要找到另一种方法.
Currently, I do a search for text I know is in the file and it works well. But I need to do it for another project where I don't know any of the contents of the resource file and I need to find another method.
FileStream exe = new FileStream(currentexe, FileMode.Open);
//find xml part of exefile
string find = "<?xml version="1.0"?>";
string lastchars = new string(' ', find.Length);
while (exe.CanRead) {
lastchars = lastchars.Substring(1) + (char)exe.ReadByte();
if (lastchars == find) {
exe.Seek(-find.Length, SeekOrigin.Current);
break;
}
}
//output serialized script
int bytenum = 0;
foreach (byte c in xml) {
if (c == 0) break;
exe.WriteByte(c);
bytenum++;
}
//clean out extra data
while (bytenum++ < ScriptFileSize) {
exe.WriteByte(0x20);
}
exe.Close();
您可以使用 Cecil
打开程序集并插入资源(我这样做).YMMV
You could use Cecil
to open the assembly and insert a resource (I do). YMMV
这篇关于以编程方式将资源嵌入 .NET 程序集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!