winform 读取TXT文件 放在Label中 分类: WinForm 2014-07-31 09:56 310人阅读 评论(0) 收藏

时间:2022-05-12 09:11:47
<span style="font-family: Arial, Helvetica, sans-serif;">#region 读取TXT 文件,放到Label中</span>
        private void ReadTXT(Label lab) {
string strTxtAll = "";//定义一个string变量
string abc = "C:\Users\xxb\Desktop\1.txt";//路径
FileStream fs = new FileStream(abc, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
string line = sr.ReadLine();
strTxtAll = line + "\r\n";
for (int ac = 0; line != null; ac++) {
line = sr.ReadLine();
strTxtAll += line + "\r\n";
}
lab.Text = strTxtAll;
sr.Close();
fs.Close();
}
#endregion
调用即可:
  ReadTxt(label1);


版权声明:本文为博主原创文章,未经博主允许不得转载。