怎样将一个.txt文件读入到string 变量中

borwin 2004-11-10 11:28:30
怎样将一个.txt文件读入到string 变量中
...全文
172 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
龍过鸡年 2004-11-10
  • 打赏
  • 举报
回复
string content = ""; // 定义字符串
//定义文件读取对象
using(StreamReader reader = new StreamReader(fileName, System.Text.Encoding.Default))
{
content = reader.ReadToEnd(); //读取文件内容
reader.Close(); //关闭文件
}
egxsun 2004-11-10
  • 打赏
  • 举报
回复
字符串长度有限制吧?超过64k是不是就不行了?
wl246 2004-11-10
  • 打赏
  • 举报
回复
sequelbo(那一剑的风情)方法很好,比较简单
sequelbo 2004-11-10
  • 打赏
  • 举报
回复
private string getstring(string path)
{
System.IO.FileStream fs = System.IO.File.OpenRead(path);
byte[] file = new Byte[fs.Length];
fs.Read(file,0,Convert.ToInt32(fs.Length));
string s = System.Text.Encoding.Default.GetString(file,0,file.Length);
fs.Close();
return s;
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧