我正在尝试使用读取嵌入式文本文件,System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
但它给了我一个Stream
。嵌入式资源是一个文本文件,因此如何将其Stream
转换为TextReader
?
Answers:
TextReader tr = new StreamReader(stream);
您可以像这样读取资源文件:
Assembly resourceAssembly = Assembly.LoadFrom(resourceFile.FullName);
string[] manifests = resourceAssembly.GetManifestResourceNames();
using (ResourceReader reader = new ResourceReader(
resourceAssembly.GetManifestResourceStream(manifests[0])))
{
foreach (IDictionaryEnumerator dict in reader)
{
string key = dict.Key as string;
object val = dict.Value;
}
}
foreach
环比的显式枚举更好。