using System;
using System.IO;
using System.Text;
namespace CB.Common
{
public class FileUtil
{
///
/// 记录日志
///
///
public static void WriteLog(string msg)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Log.txt";
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine(System.DateTime.Now.ToString() + " " + msg);
sw.Close();
}
}
///
/// 写入文件
///
///
///
/// 是否追加写入
public static void WriteFile(string fileName, string content, bool append = false)
{
using (StreamWriter sw = new StreamWriter(fileName, append, Encoding.UTF8))
{
sw.Write(content);
sw.Flush();
sw.Close();
}
}
///
/// 读取文件内容
///
///
///
///
public static string ReadFile(string fileName, Encoding encoding)
{
string result = "";
using (StreamReader sr = new StreamReader(fileName, encoding))
{
result = sr.ReadToEnd();
}
return result;
}
///
/// 读取文本文件
///
/// 文件路径
///
public static string ReadTextFile(string filepath)
{
try
{
if (File.Exists(filepath))
{
return File.ReadAllText(filepath);
}
else
{
return "";
}
}
catch //(Exception e)
{
return "";
}
}
}
}