Assembly.GetManifestResourceStream 总是 returns 空
Assembly.GetManifestResourceStream always returns null
每当我完成 Path-Resolution
的任务时,我总是感到困惑。关于报告的问题,我有以下信息:
- 使用
ASP.NET MVC-5 Application
- 正在尝试通过以下代码访问
font file
即 MyriadPro-SemiBold.ttf
//a value receives a path + name of the file in variable i.e. name
string name = "myApplicationName.fonts.MyriadPro-Semibold.ttf";
//object (named as assembly) of class System.Reflection.Assembly.
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
// stream always gets null value (don't know why!)
{
if(stream != null)
{
//myCode waiting for above stream not to be null :-(
}
else
{
throw new ArgumentException("No resource with name " + name);
}
}
我不太了解 Visual Studio 在 paths
方面在不同类型应用程序中的工作方式。
您的资源应该被嵌入:
你可以做一个小测试来获取你所有的资源并找到好名字。之后你的代码似乎是正确的。
var allRessources= System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("fullpath here");
if (stream == null) return;
编辑
要在 VS 项目中添加文件作为嵌入式资源:只需将文件添加到项目中,单击它,然后在 Properties 下设置 Build Action 到 嵌入式资源 。就是这样!
有关嵌入式资源的更多信息:https://support.microsoft.com/en-us/kb/319292#bookmark-4
每当我完成 Path-Resolution
的任务时,我总是感到困惑。关于报告的问题,我有以下信息:
- 使用
ASP.NET MVC-5 Application
- 正在尝试通过以下代码访问
font file
即MyriadPro-SemiBold.ttf
//a value receives a path + name of the file in variable i.e. name
string name = "myApplicationName.fonts.MyriadPro-Semibold.ttf";
//object (named as assembly) of class System.Reflection.Assembly.
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
// stream always gets null value (don't know why!)
{
if(stream != null)
{
//myCode waiting for above stream not to be null :-(
}
else
{
throw new ArgumentException("No resource with name " + name);
}
}
我不太了解 Visual Studio 在 paths
方面在不同类型应用程序中的工作方式。
您的资源应该被嵌入:
你可以做一个小测试来获取你所有的资源并找到好名字。之后你的代码似乎是正确的。
var allRessources= System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("fullpath here");
if (stream == null) return;
编辑
要在 VS 项目中添加文件作为嵌入式资源:只需将文件添加到项目中,单击它,然后在 Properties 下设置 Build Action 到 嵌入式资源 。就是这样!
有关嵌入式资源的更多信息:https://support.microsoft.com/en-us/kb/319292#bookmark-4