/// <summary> /// 寫入記事本 /// </summary> /// <param name="log">日志內容</param> /// <param name="filepath">文件路徑(含文件名)</param> /// <returns></returns> private static bool WriteTxt(string log, string filepath) { try { string folder = filepath.Substring(0, filepath.LastIndexOf('\\')); // 創建目錄 if (Directory.Exists(folder) == false) { Directory.CreateDirectory(folder); } // 當文件不存在時創建文件 if (File.Exists(filepath) == false) { FileStream fs = File.Create(filepath); fs.Close(); } // 寫入文件內容 File.AppendAllText(filepath, "【" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "】" + log + "\r\n", Encoding.Default); return true; } catch { return false; } }
