C#从资源读取字节数组 [英] c# read byte array from resource

查看:251
本文介绍了C#从资源读取字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出如何从我的一个资源文件中读取字节数组,我尝试了在Google上最受欢迎的点击,但未获得成功。

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
}

这篇关于C#从资源读取字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆