C#读取TXT文件

来源:互联网 发布:m20火箭筒数据 编辑:程序博客网 时间:2024/06/06 03:54

前台文件

<asp:Label runat="server" ID="label" Text=""></asp:Label>

后台文件

//添加命名空间using System.IO;using System.Text;
    protected void Page_Load(object sender, EventArgs e)    {        if (!this.IsPostBack)        {            string Path = Server.MapPath("ReadTxt.txt");//读取Txt文件的路径            insertStr(Path);        }    }    public void insertStr(string Path)    {        string strLine = "";        StreamReader sr = new StreamReader(Path, Encoding.GetEncoding("GB2312"));        strLine = sr.ReadToEnd();//读取Txt文件里所有的内容        label.Text = strLine;//赋值给控件Label        sr.Dispose();        sr.Close();    }

TXT文件:



运行结果:



0 0
原创粉丝点击