我已经将sample.txt(它只包含一行"aaaa“)文件嵌入到项目的资源中,就像在这个answer中一样。当我像这样读它的时候:
string s = File.ReadAllText(global::ConsoleApplication.Properties.Resources.sample);我得到了System.IO.FileNotFoundException的例外。附加信息:找不到文件'd:\Work\Projects\MyTests\ConsoleApplication\ConsoleApplication\bin\Debug\aaaa'.
所以看起来,它试图从我的资源文件中获取文件名,而不是读取这个文件。为什么会发生这种情况?我如何让它读到sample.txt
尝试@Ryios的解决方案并获得参数空异常
  using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication.Resources.sample.txt"))
        {
            TextReader tr = new StreamReader(stream);
            string fileContents = tr.ReadToEnd();
        }该文件位于d:\Work\Projects\MyTests\ConsoleApplication\ConsoleApplication\Resources\sample.txt中
附注:解决了。我必须在sample.txt属性中设置Build embed资源
发布于 2017-07-02 19:00:43
你好,建议的解决方案不起作用
此返回null
Assembly.GetExecutingAssembly().GetManifestResourceStream("xyz.project.Folder1.Folder2.SomeFile.Txt")另一种方法是使用来自MemoryStream的Ressource Data
  byte[] aa = Properties.Resources.YOURRESSOURCENAME;
  MemoryStream MS =new MemoryStream(aa);
  StreamReader sr = new StreamReader(MS);不太理想,但有效
https://stackoverflow.com/questions/35780084
复制相似问题