在类库项目资源类库、项目、资源

2023-09-04 08:38:08 作者:青丝成雪

我想一些XML文件作为资源添加到我的类库项目。 任何想法如何做到这一点的,后来打电话吗? 在Windows应用程序中,我会做这样 ClassLibrary1.Properties.Resources.file.xml

不过,这并没有任何工作的想法我怎么在这里?

解决方案

本文解释使用嵌入的资源在C#。

它归结为

在设计时间:将文件添加到项目并将其标记为嵌入的资源(属性) 在运行时间:获得大会的情况下,保存你想要的资源,然后让该资源作为一个流   

VAR组装= Assembly.GetExecutingAssembly();

     

VAR流= _assembly.GetManifestResourceStream(fully.qualified.name.of.the.resource);

如果你正在努力制定出资源的FQ名字,一个偷懒的办法是使用的反射来打开保存它的组件。转到资源文件夹,右键单击特定资源,选择复制。

I want to add some xml file as resource to my class library project . Any idea how to do so , and call it later? In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml

窗体项目 生成dll类库文件

But here it didn't worked any idea how i do it here ?

解决方案

This article explain how to use embedded resources in C#.

It boils down to

At 'Design time': Add file to project and mark it as an embedded resource (properties) At 'Run time': Get instance of Assembly that holds the resource you want, then get that resource out as a stream.

var assembly = Assembly.GetExecutingAssembly();

var stream = _assembly.GetManifestResourceStream("fully.qualified.name.of.the.resource");

If you're struggling to work out the fq name of the resource, a lazy way is to use the reflector to open the assembly that holds it. Go to the Resources folder, right click on the particular resource and choose 'Copy'.