c#读取/写入文件

王朝学院·作者佚名  2010-04-26  
宽屏版  字体: |||超大  

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

/**//// <summary>

/// 检查文件是否存在

/// </summary>

/// <param name="FilePath"></param>

private void ExistsFile(string FilePath)

{

if (!File.Exists(FilePath))

{

File.Create(FilePath);

}

}

//读取文件

protected void Button2_Click(object sender, EventArgs e)

{

ExistsFile(Server.MapPath("~/test/111.txt"));

//读取文件ASP.NET

StreamReader sr = new StreamReader(Server.MapPath("~/test/111.txt"), System.Text.Encoding.Default);

try

{

string input = sr.ReadToEnd();

sr.Close();

input = input.Replace("<br>", "\r\n");

Response.Write("<b>文件读取成功!</b>");

TextBox1.Text = input;

}

catch

{

Response.Write("<b>文件读取失败!</b>");

}

}

//写入文件

protected void Button1_Click(object sender, EventArgs e)

{

ExistsFile(Server.MapPath("~/test/111.txt"));

//写入文件

StreamWriter sw = new StreamWriter(Server.MapPath("~/test/111.txt"), false, System.Text.Encoding.Default);

try

{

sw.Write(TextBox1.Text);

sw.Close();

Response.Write("<b>文件写入成功!</b>");

}

catch

{

Response.Write("<b>文件写入失败!</b>");

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有