【问题标题】:How to Read an embedded resource as array of bytes without writing it to disk?如何将嵌入式资源作为字节数组读取而不将其写入磁盘?
【发布时间】:2012-05-11 20:20:53
【问题描述】:

在我的应用程序中,我使用 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(); 
}

我怎样才能做同样的事情(将嵌入式资源作为字节数组获取)但不向硬盘写入任何内容?

【问题讨论】:

    标签: c# .net embedded-resource codedom system-codedom-compiler


    【解决方案1】:

    您实际上已经将流读取到一个字节数组,为什么不停在那里?

    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:请参阅 cmets 以获得更好的阅读模式。

    【讨论】:

    • 更新了我的答案,当您已经将流读取到字节数组时,不需要内存流。
    • 这可能无法正常工作,因为Stream.Read() 可能不会在一个Read() 中返回所有数据。我不确定这个特定的Stream 的行为如何,但为了安全起见,我会使用MemoryStreamCopyTo(),然后使用ToArray()
    • @svick :你是对的。或者,可以使用本页示例中描述的阅读模式:msdn.microsoft.com/en-us/library/system.io.stream.read.aspx
    【解决方案2】:

    使用MemoryStream 的简单替代方案:

    var ms = new MemoryStream();
    await resFilestream.CopyToAsync(ms);
    var bytes = ms.ToArray();
    

    【讨论】:

      【解决方案3】:

      请记住,嵌入式资源文件名 = 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;
      }
      

      【讨论】:

      • 能否将格式修改为您的初衷?
      • 值得注意的是(至少在 net5.0 上)资源名称实际上以程序集根命名空间为前缀,而不是程序集名称。因此,如果这些不同,您必须小心上述内容。
      • 我同意@banmccallum 的观点,这值得注意的。要确定对我有用的名称是使用var resources = GetType().Assembly.GetManifestResourceNames();
      【解决方案4】:

      如果您正在阅读嵌入式资源,这是一种简单的方法。

      string resourcePath = "pack://application:,,,/resource/location/S_2/{0}";
              StreamResourceInfo resInfo = Application.GetResourceStream(new Uri(resourcePath));
      
              if (resInfo == null)
              {
                  throw new Exception("Resource not found: " + resourcePath);
              }
      
              var ms = new System.IO.MemoryStream();
              await resInfo.Stream.CopyToAsync(ms);
              byte[] bytes = ms.ToArray();
      

      【讨论】:

        【解决方案5】:
        File.WriteAllBytes(@"C:\Users\admin\Desktop\MyFile.exe", Resources.BinFile); // binary file
        File.WriteAllText(@"C:\Users\admin\Desktop\text.txt", Resources.TextFile); // text file
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-13
          • 1970-01-01
          • 1970-01-01
          • 2011-05-23
          • 1970-01-01
          • 1970-01-01
          • 2011-10-26
          • 1970-01-01
          相关资源
          最近更新 更多