简体   繁体   English

c# 从资源中读取字节数组

[英]c# read byte array from resource

I've been trying to figure out how to read a byte array from one of my resource files, I have tried the most popular hits on Google without clear success.我一直在试图弄清楚如何从我的一个资源文件中读取字节数组,我已经尝试了谷歌上最受欢迎的点击但没有明显的成功。

I have a file stored in the resource collection of my program, I'd like to read this file as a byte array我有一个文件存储在我的程序的资源集合中,我想将这个文件作为字节数组读取

I'm currently just reading the file from the root directory of my program with the following code:我目前正在使用以下代码从程序的根目录中读取文件:

FileStream fs = new FileStream(Path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

However I want to store this file as a resource in my application, so that I don't have to ship an extra file with my program.但是,我想将该文件作为资源存储在我的应用程序中,这样我就不必随程序一起发送额外的文件。

This file holds encrypted data that part of my program uses.该文件包含我的部分程序使用的加密数据。

Any help or pointers would be greatly appreciated!任何帮助或指示将不胜感激!

Assuming you are talking about files that are embedded as resources in your assembly: 假设您正在讨论作为程序集中的资源嵌入的文件:

var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("SomeNamespace.somefile.png"))
{
    byte[] buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
    // TODO: use the buffer that was read
}

You can add resources to your application by going in the properties of the project, "Resources" tab (create one if needed), Add Resource (existing file). 您可以通过项目属性,“资源”选项卡(如果需要,创建一个),添加资源(现有文件),为应用程序添加资源。 When your file is added, you can set its FileType (in its properties) to Binary. 添加文件后,可以将其FileType(在其属性中)设置为Binary。

Documentation 文档

After that, you can access to your file as a byte[] easily: 之后,您可以轻松地以字节[]的形式访问您的文件:

var myByteArray = Properties.Resources.MyFile;
byte[] capacity = Properties.Resources.excel;

在此处输入图像描述

Confusion:混乱: 在此处输入图像描述

here we reading txt resource file.so it not return byte[] but it return string (file content).在这里我们读取 txt 资源文件。所以它不返回byte[]但它返回string (文件内容)。

var rm = new ResourceManager("RessourceFile", typeof(ClassXY).Assembly);
return Encoding.UTF8.GetBytes(rm.GetString("key"));

maybe you can try using StreamResourceInfo. 也许你可以尝试使用StreamResourceInfo。 Here is a link to a Silverlight example, but if I am not mistaken you should be able to apply the same principles in any .NET application: 这是一个Silverlight示例的链接,但如果我没有弄错,您应该能够在任何.NET应用程序中应用相同的原则:

http://msdn.microsoft.com/en-us/library/system.windows.resources.streamresourceinfo(v=VS.95).aspx http://msdn.microsoft.com/en-us/library/system.windows.resources.streamresourceinfo(v=VS.95).aspx

Regards, 问候,
Anders @ Cureos 安德斯@Cureos

Here's a little class we're using for this purpose: 这是我们为此目的使用的一个小课程:

static class EmbeddedResource
{
    /// <summary>
    /// Extracts an embedded file out of a given assembly.
    /// </summary>
    /// <param name="assemblyName">The namespace of your assembly.</param>
    /// <param name="fileName">The name of the file to extract.</param>
    /// <returns>A stream containing the file data.</returns>
    public static Stream Open(string assemblyName, string fileName)
    {
        var asm = Assembly.Load(assemblyName);
        var stream = asm.GetManifestResourceStream(assemblyName + "." + fileName);

        if (stream == null)
            throw new ConfigurationErrorsException(String.Format(
                    Strings.MissingResourceErrorFormat, fileName, assemblyName));

        return stream;
    }
}

Usage is pretty straightforward: 用法非常简单:

using (var stream = EmbeddedResource.Open("Assembly.Name", "ResourceName"))
    // do stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM