Build Action 设置为 Embedded Resource

来源:互联网 发布:win7固态硬盘优化工具 编辑:程序博客网 时间:2024/06/06 18:35

Microsoft .NET 框架支持两种资源 — 无类型清单资源和有类型资源。通过将文件的 Build Action 设置为 Embedded Resource,可以让 Visual Studio .NET 支持无类型清单资源,并通过 .resx 文件(可以是自定义文件或作为组件资源的备份存储)支持有类型资源。清单资源的好处是,它们可在 IDE 中直接编辑,而有类型资源需要做特别的工作才能编辑,但可提供有类型访问。两种资源类型都有某些严格的命名要求,所以在编写方法调用来加载它们时要格外小心。

读取无类型清单资源
var ass = Assembly.GetExecutingAssembly();

//查询所有的Embedded Resource
foreach (var file in ass.GetManifestResourceNames())
Console.WriteLine(file);

//you can find resourceName above, always like this
//..
var stream = ass.GetManifestResourceStream(resourceName );

byte[] buffer = new byte[stream.Length];

stream.Read(buffer, 0, buffer.Length);

File.WriteAllBytes(fileName, buffer);

有类型资源指Resource.resx文件,请参考
http://blog.csdn.net/kevindr/article/details/46722437

0 0