.net 如何将嵌入式资源作为字节数组读取而不将其写入磁盘?

5q4ezhmt  于 2023-10-21  发布在  .NET
关注(0)|答案(5)|浏览(122)

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

我怎么能做同样的事情(获取字节数组的嵌入式资源),但不写任何东西到硬盘?

bq3bfh9z

bq3bfh9z1#

实际上,您已经在将流阅读到字节数组中,为什么不就此停止呢?

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;
    }
}

编辑:查看评论以获得更好的阅读模式。

czfnxgou

czfnxgou2#

使用MemoryStream的简单替代方案:

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

0s7z1bwu3#

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

xfyts7mz4#

如果你正在阅读一个嵌入的资源,这里有一个简单的方法。

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();
pokxtpni

pokxtpni5#

File.WriteAllBytes(@"C:\Users\admin\Desktop\MyFile.exe", Resources.BinFile); // binary file
File.WriteAllText(@"C:\Users\admin\Desktop\text.txt", Resources.TextFile); // text file

相关问题