首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在.Net压缩框架中将文件内容读取为字符串

在.Net压缩框架中将文件内容读取为字符串
EN

Stack Overflow用户
提问于 2011-07-28 04:31:53
回答 4查看 95.1K关注 0票数 50

我正在使用.net compact Framework2.0为移动设备开发一个应用程序。我试图将文件的内容加载到string对象中,但不知何故无法完成。System.IO.StreamReader类中没有ReadToEnd()方法。是否有其他类提供此功能?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-28 04:33:46

代码语言:javascript
复制
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader("TestFile.txt")) 
{
    String line;
    // Read and display lines from the file until the end of 
    // the file is reached.
    while ((line = sr.ReadLine()) != null) 
    {
        sb.AppendLine(line);
    }
}
string allines = sb.ToString();
票数 99
EN

Stack Overflow用户

发布于 2012-07-24 05:27:58

代码语言:javascript
复制
string text = string.Empty;
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
{            
    text = streamReader.ReadToEnd();
}

另一种选择:

代码语言:javascript
复制
string[] lines = File.ReadAllLines("file.txt");

https://gist.github.com/paulodiogo/9134300

很简单!

票数 41
EN

Stack Overflow用户

发布于 2011-07-28 04:34:27

File.ReadAllText(file)你要找的是什么?

如果您喜欢将其按行分解为数组,也可以使用File.ReadAllLines(file)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6850850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档