在C#中怎么通过代码打开文本文件将内容取进TEXTBOX,急,拜托大家了!!!

smallbee008 2003-10-09 07:07:24
在C#中怎么通过代码打开文本文件将内容取进TEXTBOX,急,拜托大家了!!!
...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狼1234567 2003-10-10
  • 打赏
  • 举报
回复
using System;
using System.IO;
public class TextFromFile {
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args) {
if (!File.Exists(FILE_NAME)) {
return;
}
StreamReader sr = File.OpenText(FILE_NAME);
String input;
while ((input=sr.ReadLine())!=null) {
//input................
}

sr.Close();
}
}


using System;
using System.IO;
public class TextToFile {
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args) {
if (File.Exists(FILE_NAME)) {
return;
}
StreamWriter sr = File.CreateText(FILE_NAME);
sr.WriteLine ("字符串");
sr.Close();
}
}
skykevin 2003-10-10
  • 打赏
  • 举报
回复
打开文本文件将内容取进多个TEXTBOX
string FILE_NAME = "AppCfg.txt";
if (!File.Exists(FILE_NAME))
{
MessageBox.Show("配置文件不存在!", FILE_NAME);
return;
}
StreamReader sr = File.OpenText(FILE_NAME);
foreach(Control ctrl in this.Controls)
{
if(ctrl is TextBox)
ctrl.Text=sr.ReadLine();
}
sr.Close();
graying 2003-10-09
  • 打赏
  • 举报
回复
楼上两位说得差不多了.但是我记得好像应该是FileStreamObject类啊,然后再while(...ReadLine())
meiyoufangxiang 2003-10-09
  • 打赏
  • 举报
回复
详细!
himuraz 2003-10-09
  • 打赏
  • 举报
回复
//这个要比上面的效率要高
using System.IO;
using System.Text;
//
//
FileStream fs=new FileStream("filepath", FileMode.Append, FileAccess.Write, FileShare.Write);
StreamReader sr= new StreamReader(fs);
StringBuilder strbuilder= new StringBuilder(53);
string str=null;
while((str=Sr.ReadLine())!=null)
{
strbuilder.Append(str);
}
testBox.text = strbuilder.Tostring();

sr.Close();
fs.Close();

//大致就是这样
himuraz 2003-10-09
  • 打赏
  • 举报
回复
using System.IO;
//
//
FileStream fs=new FileStream("filepath", FileMode.Append, FileAccess.Write, FileShare.Write);
StreamReader sr= new StreamReader(fs);
string str=null;
while((str=Sr.ReadLine())!=null)
{
testBox.text += str;
}
sr.Close();
fs.Close();


我不懂电脑 2003-10-09
  • 打赏
  • 举报
回复
用RichTextBox控件吧
richTextBox1.LoadFile(fileName);

110,533

社区成员

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

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

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