1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.IO;
- using System.Text;
- namespace CB.Common
- {
- public class FileUtil
- {
- /// <summary>
- /// 记录日志
- /// </summary>
- /// <param name="msg"></param>
- 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();
- }
- }
- /// <summary>
- /// 写入文件
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="content"></param>
- /// <param name="append">是否追加写入</param>
- 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();
- }
- }
- /// <summary>
- /// 读取文件内容
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="encoding"></param>
- /// <returns></returns>
- public static string ReadFile(string fileName, Encoding encoding)
- {
- string result = "";
- using (StreamReader sr = new StreamReader(fileName, encoding))
- {
- result = sr.ReadToEnd();
- }
- return result;
- }
- /// <summary>
- /// 读取文本文件
- /// </summary>
- /// <param name="filepath">文件路径</param>
- /// <returns></returns>
- public static string ReadTextFile(string filepath)
- {
- try
- {
- if (File.Exists(filepath))
- {
- return File.ReadAllText(filepath);
- }
- else
- {
- return "";
- }
- }
- catch //(Exception e)
- {
- return "";
- }
- }
- }
- }
|