C# equivalent of getClassLoader().getResourceAsStream(…)

会有一股神秘感。 提交于 2019-12-01 06:37:48

问题


In Java you can read a file embedded inside a JAR-file by using the following code:

String file = "com/company/package/filename.txt";
InputStream is = ClassName.class.getClassLoader().getResourceAsStream(file);

What is the C#/.NET equivalent of the above code - that is, how do I read a file I've embedded inside a DLL?

Thanks!


回答1:


Once you've added the text file as a resource, and assigned a resourceName to it, then:

 Assembly assembly = Assembly.GetExecutingAssembly();
 TextReader inputStream = new StreamReader(assembly.GetManifestResourceStream(resourceName));
 string result = inputStream.ReadToEnd();

Note: this came from this posting



来源:https://stackoverflow.com/questions/474055/c-sharp-equivalent-of-getclassloader-getresourceasstream

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