首页 > 代码库 > winform 读取TXT文件 放在Label中

winform 读取TXT文件 放在Label中

<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);