Embedded resource file not present at runtime .Net 4

丶灬走出姿态 提交于 2021-02-08 05:40:45

问题


I have a file I need to access at runtime, I've included it in my project and set it up as embedded resource (it's actually a source file, I changed the extension to .cs.txt to get around VS trying to compile it. That shouldn't matter, but I'm mentioning it anyway just in case). When I try getting the file

var assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(resourceName);

I get a null. I've made sure I'm using the Namespace.Folder.Filename notation, but that didn't help. It appears the file is actually not there, because when I call

assembly.GetManifestResourceNames();

I get an empty string array. Any idea what could be the case?


回答1:


So I got around this by using the VS resource manager. Now I can access the file directly like this:

MyNamespace.Properties.Resources.MyFile

I'd recommend this approach to anyone, as it seems not only much cleaner, but safer as well. Thanks Hans Passant for the advice.



来源:https://stackoverflow.com/questions/25773550/embedded-resource-file-not-present-at-runtime-net-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!