关于Log文本的操作

// <summary>
/// 创建文本
/// </summary>
/// <param name="ID"></param>
/// <param name="content"></param>
private static void AssetsWrite(string ID, string content)
{
try
{
string path = Path.Combine(AssetsPath, ID + ".log");
if (File.Exists(path))
{
throw new AthException("文件已存在");
}
else
{
FileStream myFs = new FileStream(path, FileMode.Create);
StreamWriter mySw = new StreamWriter(myFs, Encoding.UTF8);
mySw.Write(content);
mySw.Close();
myFs.Close();
}
}
catch (AthException athex)
{
throw athex;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 读取文本内容
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static string FileReadContent(string path)
{
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
StringBuilder sb = new StringBuilder();
string content = sr.ReadLine();
sr.Close();
return content;
}
catch (Exception ex)
{
throw ex;
}

}

/// <summary>
/// 获取文件夹文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static List<string> GetFileName(string path)
{

List<string> logList = new List<string>();
try
{
if (!string.IsNullOrEmpty(path))
{
DirectoryInfo folder = new DirectoryInfo(path);
foreach (FileInfo file in folder.GetFiles("*.log"))
{
logList.Add(file.Name);
}
}
else
{
throw new AthException("路径不完善");
}
}
catch (AthException athex)
{
throw athex;
}
catch (Exception)
{

throw;
}

return logList;
}

 

/// <summary>
/// 删除文件
/// </summary>
/// <param name="path"></param>
private static void DeleteFile(string path)
{
try
{
if (!File.Exists(path)) return;
FileAttributes attr = File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
//删除文件夹
Directory.Delete(path, true);
}
else
{
//删除文件
File.Delete(path);
}
}
catch (Exception ex)
{
throw ex;
}

}

}

posted @ 2018-08-08 14:51  D调灬仔  阅读(187)  评论(0编辑  收藏  举报

作者D调灬仔

出处:https://www.cnblogs.com/chj929555796/

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!