想让以下代码有效
System::IO::Stream^ res;
res = System::Reflection::Assembly::GetExecutingAssembly()->GetManifestResourceStream("MyGame.text.txt"); /*每次这里都返回NULL*/
StreamReader^ pReader=gcnew StreamReader(res, System::Text::Encoding::UTF8);
String^ s=pReader->ReadLine();
回答:
在工程里添加一个托管的资源文件.resx
在这个.resx里面添加txt文件
然后用如下代码获取
Assembly^ assembly = Assembly::GetExecutingAssembly();
ResourceManager^ rm = gcnew ResourceManager("这里填你的程序集名.(注意这里有个点)这里填resx文件的文件名 不要后缀", assembly);
String^ s=(System::String^)rm->GetObject("这里填txt的文件名 同样不要.txt后缀");
这样就把整个txt读进去了
之后再在这个string里处理
本文介绍了一种在C#中有效读取应用程序内嵌资源文件的方法,特别是.txt文件。通过使用资源文件(.resx)并结合Assembly和ResourceManager类,可以轻松地将整个文本文件内容读取到字符串变量中。

9万+

被折叠的 条评论
为什么被折叠?



