前台文件
<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文件:
运行结果: