/// <summary>
/// 内容写入到文本文件
/// </summary>
/// <param name="fileName">文件名称</param>
/// <param name="content">内容</param>
/// <returns>返回错误信息或时1代表写入成功</returns>
public string writeFile(string fileName,string content)
{
try
{
string pathName = Server.MapPath(fileName);
//这个不存在会自动创建 存在就直接覆盖了 当false为true时 就是追加了
using (StreamWriter sw = new StreamWriter(pathName, false, Encoding.Default))
{
sw.Write(content);
sw.Flush();
sw.Close();
}
}
catch (Exception ex)
{
return ex.Message;
}
return "1";
}
/// <summary>
/// 读取文本文件内容
/// </summary>
/// <param name="fileName">文件名称</param>
/// <returns>返回内容或o(测试的时候可以返回ex.Message)</returns>
[AjaxPro.AjaxMethod]
public string readFile(string fileName)
{
string result = "";
try
{
string pathName = Server.MapPath(fileName);
using (StreamReader sr = new StreamReader(pathName, Encoding.Default))
{
result=sr.ReadToEnd();
sr.Close();
}
}
catch (Exception ex)
{
return "0";//ex.Message;
}
return result;
}
转载于:https://www.cnblogs.com/0banana0/archive/2011/10/10/2205126.html