从stream中获取TextReader?

我试图用System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);读取embedded式文本文件System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource); 但它给了我一个Stream 。 embedded的资源是一个文本文件,所以,我怎样才能把这个Stream成为一个TextReader

 TextReader tr = new StreamReader(stream); 

一个StreamReaderTextReader一个子类,所以你可以这样做:

 using(var stream = System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream(resource)) using(var reader = new StreamReader(stream)) { // Use reader. } 

你可以像这样读取你的资源文件:

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

您需要从中获取StreamReader(TextReader是StreamReader和StringReader的抽象基类)。 就像是:

 var stream = System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream(resource); var reader = new StreamReader(stream); var text = reader.ReadToEnd();