C#相当于getClassLoader

C#相当于getClassLoader

本文介绍了C#相当于getClassLoader()。getResourceAsStream(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,您可以使用以下代码来读取JAR文件中嵌入的文件:

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);

与上述代码相当的C#/ .NET是什么 - 也就是说,如何读取文件我已经嵌入到DLL中?

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

谢谢!

推荐答案

将文本文件添加为资源后,分配一个 resourceName ,然后:

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();

注意:这来自发布

这篇关于C#相当于getClassLoader()。getResourceAsStream(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:37