问一个从文本文件读出数据后的显示问题!
一开始我建立一个log.txt文件,然后输入内容,代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
if(!File.Exists(@"C:\Inetpub\wwwroot\ThisWeek\log.txt"))
{
File.Create(@"C:\Inetpub\wwwroot\ThisWeek\log.txt");
this.lblTextFileName.Text="log.txt";
Response.Write("<script>alert('文件创建成功!!!')</script>");
}
}
private void btnInsertTxt_Click(object sender, System.EventArgs e)
{
using (StreamWriter sw = new StreamWriter(@"C:\Inetpub\wwwroot\ThisWeek\log.txt"))
{
// Add some text to the file.
sw.Write("插入时间为:");
sw.WriteLine(DateTime.Now);
sw.WriteLine("你输入的文本内容为:");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
string sMystr="";
sMystr=this.txtData.Text.ToString();
sw.WriteLine(sMystr);
}
Response.Write("<script>alert('文本添加成功!!!')</script>");
}
接着我想把我输入的内容在一个textbox中显示一下:
private void btnShow_Click(object sender, System.EventArgs e)
{
try
{
// Open the file to read from.
using (StreamReader sr = File.OpenText(@"C:\Inetpub\wwwroot\ThisWeek\log.txt"))
{
this.txtShow.Text="";
string sReadText = "";
while ((sReadText = sr.ReadLine()) != null)
{
this.txtShow.Text+=sReadText.ToString();
}
}
}
catch (Exception excep)
{
// Let the user know what went wrong.
Response.Write("<script>alert('无此文件!!!')</script>");
}
}
结果得到的是按行输出的内容,我想要的格式是,按照输入的格式!
如果文本文件内容为:
插入时间为:2006-10-25 10:40:54
你输入的文本内容为:
-------------------
guozi
guozi
guozi
guozi
guozi
guozi
guozi
guozi
而在textbox显示的为:
插入时间为:2006-10-25 10:40:54 你输入的文本内容为:-------------------guoziguoziguoziguoziguozi
我想要文本文件内的内容与格式,不知道怎么得到,谢谢