GetManifestResourceStream返回NULL [英] GetManifestResourceStream returns NULL

查看:923
本文介绍了GetManifestResourceStream返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个C#.NET 4.0的应用程序:

This is a C# .NET 4.0 application:

我嵌入文本文件作为资源,然后尝试在对话框中显示它:

I'm embedding a text file as a resource and then trying to display it in a dialog box:

    var assembly = Assembly.GetExecutingAssembly();
    var resourceName = "MyProj.Help.txt";

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string result = reader.ReadToEnd();
                System.Windows.Forms.MessageBox.Show(result, "MyProj", MessageBoxButtons.OK);
            }
        }



解决方案是MyProjSolution和可执行是MyProj.exe 。的help.txt是一个嵌入式的资源。但是,该流为空。我试过MyProjSolution.Help.txt和MyProjSolution.MyProj.Help.txt但似乎没有任何工作。

The solution is MyProjSolution and the executable is MyProj.exe. Help.txt is an embedded resource. However, the stream is null. I've tried MyProjSolution.Help.txt and MyProjSolution.MyProj.Help.txt but nothing seems to work.

推荐答案

可以检查资源被正确嵌入使用

You can check that the resources are correctly embedded by using

//From the assembly where this code lives!
this.GetType().Assembly.GetManifestResourceNames()

//or from the entry point to the application - there is a difference!
Assembly.GetExecutingAssembly().GetManifestResourceNames()



在调试的时候。这将列出嵌入到你的代码写在程序集中的所有资源的所有(完全合格的名称)

when debugging. This will list all the (fully qualified names) of all resources embedded in the assembly your code is written in.

链接: http://msdn.microsoft.com/en-us/library/system.reflection.assembly .getmanifestresourcenames(v = vs.110)的.aspx

只需复制相关的名称,并用它来代替无论你在变量定义资源名称。

Simply copy the relevant name, and use that instead of whatever you have defined in the variable 'resourceName'.

注 - 资源名称是区分大小写的,如果你没有正确的嵌入式资源文件,它不会被调用返回GetManifestResourceNames列表中显示出来() 。同时 - 确保你从正确装配读取资源(如果使用了多个组件) - 这是所有容易从当前执行的程序集,而不是从引用的程序集

Notes - the resource name is case sensitive, and if you have incorrectly embedded the resource file, it will not show up in the list returned by the call to GetManifestResourceNames(). Also - make sure you are reading the resource from the correct assembly (if multiple assemblies are used) - it's all to easy to get the resources from the currently executing assembly rather than from a referenced assembly.

这篇关于GetManifestResourceStream返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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