16,717
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 写文本内容
/// </summary>
/// <param name="StrFileName">文本名</param>
/// <param name="StrOutPutStr">写入内容</param>
public static bool WriteTxtFile(string StrFileName, string StrOutPutStr)
{
string StrPath = string.Empty; //定义存放目录变量
StrPath = Path.GetDirectoryName(System.Web.HttpContext.Current.Request.PhysicalPath) + @"\Log\" + StrFileName + ".txt";
//判断是否有当前文件,如果有,则删除
try
{
if (File.Exists(StrPath))
{
//直接写内容文本
using (StreamWriter ObjStreamWriter = File.AppendText(StrPath))
{
ObjStreamWriter.WriteLine(StrOutPutStr);
}
}
else
{
//创建文件并写内容文本
using (StreamWriter ObjStreamWriter = File.CreateText(StrPath))
{
ObjStreamWriter.WriteLine(StrOutPutStr);
}
}
return true;
}
catch(Exception ex)
{
StrErrMessage = ex.Message;
return false;
}
}
/// <summary>
/// 读文本内容
/// </summary>
/// <param name="StrFileName">文本名</param>
/// <returns>返回文本内容</returns>
public static string RreadTxtFile(string StrFileName)
{
string StrPath = string.Empty; //定义存放目录变量
StrPath = Path.GetDirectoryName(System.Web.HttpContext.Current.Request.PhysicalPath) + @"\Log\" + StrFileName + ".txt";
//判断是否有当前文件,如果有,则删除
if (File.Exists(StrPath))
{
//直接写内容文本
using (StreamReader ObjStreamReader = File.OpenText(StrPath))
{
return ObjStreamReader.ReadToEnd();
}
}
else
return null;
}