Reading from a text file resource which is compiled into a .NET assembly
Possible Duplicate:
How to read embedded resource text file
号
我尝试创建一个简单的应用程序,它从一个"资源"文件中读取,我已经将该文件添加到我的解决方案中。我试过了,但不管用:
1 2 3 4 5 6 7
| static void Main (string[] args )
{
StreamResourceInfo streamResourceInfo = Application .GetContentStream(new Uri ("pack://application:,,,/myfile.txt", UriKind .Absolute));
StreamReader sr = new StreamReader (streamResourceInfo .Stream);
var content = sr .ReadToEnd();
Console .WriteLine(content );
} |
它说:"无效的URI:指定的端口无效。"
我怎么解决这个问题?
- 是否检查了如何读取嵌入的资源文件?我想这应该能帮到你!
- 谢谢!它帮了很多忙!
我的最终解决方案:
1 2 3 4 5 6 7 8 9 10 11
| class Program
{
static void Main (string[] args )
{
Stream stream = typeof(Program ).Assembly.GetManifestResourceStream("TheNameOfMyProject.TheNameOfSubFolder.file.txt");
StreamReader sr = new StreamReader (stream );
var content = sr .ReadToEnd();
Console .WriteLine(content );
Console .ReadLine();
}
} |