GetManifestResourceStream()返回null

时间:2015-06-18 17:41:20

标签: c# winforms resources .net-4.5 embedded-resource

我试图在我的应用程序的资源中嵌入一些文本文件但是当我尝试使用GetManifestResourceStream()检索它时,我得到了null。我已尝试过所有这些名称组合:

  • programName.Form1.foo.txt
  • programName.foo.txt
  • Form1.foo.txt
  • programName.Properties.Resources.resources.foo.txt

一些信息:

命名空间为programName,我调用此代码的类别为Form1,我尝试检索的文件名资源为foo.txt

代码:

var resourceName = string.Format("???.{0}", filename);
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

使用Properties.Resources.foo可以使用该文件,但是当我从字符串中获取文件名时我无法使用它。

1 个答案:

答案 0 :(得分:3)

确保文件的构建操作是“嵌入式资源” 如果您的文件位于文件夹中,则必须包含文件夹的名称。

例如,如果我在WindowsFormsApplication1项目中创建文件夹“xx”并添加文件“dictionary.txt”,则名称将为 的 WindowsFormsApplication1.xx.dictionary.txt

无论如何只需使用它来查看所有资源

var files = Assembly.GetExecutingAssembly().GetManifestResourceNames();