| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using ivf_tl_Entity.Enums;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ivf_tl_Service
- {
- public class CSVHelper
- {
- //目录
- public static string path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\incubator_logs\\";
- //死锁
- private static object loglock = new object();
- public static void Write(string filename, string content)
- {
- lock (loglock)
- {
- try
- {
- if (!Directory.Exists(path))//如果日志目录不存在就创建
- {
- Directory.CreateDirectory(path);
- }
- //创建或打开日志文件,向日志文件末尾追加记录
- StreamWriter mySw = File.AppendText(path + filename);
- //向日志文件写入内容
- string write_content = content;
- mySw.WriteLine(write_content);
- //关闭日志文件
- mySw.Close();
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// 写文件,覆盖false,追加true
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <param name="content">内容</param>
- /// <param name="isAppend">覆盖false,追加true</param>
- public static void WriteFile(string filename, string content, bool isAppend)
- {
- lock (loglock)
- {
- try
- {
- if (isAppend)
- {
- if (!File.Exists(filename))
- {
- return;
- }
- }
- else
- {
- FileStream fs = System.IO.File.Create(filename);
- fs.Close();
- }
- using (System.IO.StreamWriter mySw = new System.IO.StreamWriter(filename, isAppend, System.Text.Encoding.UTF8))
- {
- mySw.WriteLine(content);
- mySw.Flush();
- mySw.Close();
- }
- }
- catch (Exception ex)
- {
- Log4netHelper.WriteLog("写CSV文件", ex);
- }
- }
- }
- }
- }
|