C# 如何将嵌入的资源作为字节数组读取而不将其写入磁盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10412401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Read an embedded resource as array of bytes without writing it to disk?
提问by Rafik Bari
In my application I compile another program from source.cs file using CodeDom.Compiler and I embed some resources ( exe and dll files ) at compile time using :
在我的应用程序中,我使用 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
In the compiled file, I need to read the embedded resources as array of bytes. Now I'm doing that by extracting the resources to disk using the function below and the use
在编译的文件中,我需要将嵌入的资源作为字节数组读取。现在我通过使用下面的函数和使用将资源提取到磁盘来做到这一点
File.ReadAllBytes("extractedfile.exe");
File.ReadAllBytes("extracteddll.dll");
I do this after extracting the two files to disk using this function :
我使用此功能将两个文件解压缩到磁盘后执行此操作:
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();
}
How can I do the same thing (Get the embedded resources as array of bytes) but without writing anything to hard disk?
我怎样才能做同样的事情(将嵌入的资源作为字节数组获取)但不向硬盘写入任何内容?
采纳答案by Rotem
You are actually already reading the stream to a byte array, why not just stop there?
您实际上已经在将流读取到字节数组中,为什么不就此止步呢?
public static byte[] ExtractResource(String filename)
{
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
using (Stream resFilestream = a.GetManifestResourceStream(filename))
{
if (resFilestream == null) return null;
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
return ba;
}
}
edit: See comments for a preferable reading pattern.
编辑:请参阅评论以获得更好的阅读模式。
回答by Blue Clouds
Keep in mind that Embedded resource filename = Assemblyname.fileName
请记住,嵌入式资源文件名 = Assemblyname.fileName
string fileName = "test.pdf";
System.Reflection.Assembly a =
System.Reflection.Assembly.GetExecutingAssembly();
string fileName = a.GetName().Name + "." +
"test.pdf";
using (Stream resFilestream =
a.GetManifestResourceStream(fileName))
{
if (resFilestream == null) return null;
byte[] ba = new
byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
var byteArray= ba;
}

