在我的应用程序中,我使用 CodeDom.Compiler 从 source.cs 文件编译另一个程序,并在编译时使用以下方法嵌入一些资源(exe 和 dll 文件):
// .... rest of code
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add("MyFile.exe");
}
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add("New.dll");
}
// ....rest of code
在编译的文件中,我需要将嵌入的资源读取为字节数组。现在我通过使用下面的函数和使用将资源提取到磁盘来做到这一点
File.ReadAllBytes("extractedfile.exe");
File.ReadAllBytes("extracteddll.dll");
我使用此功能将两个文件提取到磁盘后执行此操作:
public static void ExtractSaveResource(String filename, String location)
{
// Assembly assembly = Assembly.GetExecutingAssembly();
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
// Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever
// string my_namespace = a.GetName().Name.ToString();
Stream resFilestream = a.GetManifestResourceStream(filename);
if (resFilestream != null)
{
BinaryReader br = new BinaryReader(resFilestream);
FileStream fs = new FileStream(location, FileMode.Create); // say
BinaryWriter bw = new BinaryWriter(fs);
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
bw.Write(ba);
br.Close();
bw.Close();
resFilestream.Close();
}
// this.Close();
}
我怎样才能做同样的事情(将嵌入式资源作为字节数组获取)但不向硬盘写入任何内容?