.NET 创建并写CSV文件

时间:2021-07-24 21:07:17
  /// <summary>
/// 创建并写日志
/// </summary>
/// <param name="SuccessA100"></param>
/// <param name="Result"></param>
public void WriteLog(List<string> SuccessA100,string Result,string A102)
{
if (SuccessA100 == null)
{
return;
}
string txtPath = AppDomain.CurrentDomain.BaseDirectory + "log\\" +A102 + "\\"+ Result +A102.Replace("-", "")+DateTime.Now.ToString("yyyyyMMdd") + ".csv"; if (!File.Exists(txtPath))
{
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log\\" + A102 ))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log\\" + A102);
} FileStream fs1 = new FileStream(txtPath, FileMode.Create, FileAccess.Write);//创建写入文件
using (StreamWriter sw = new StreamWriter(fs1))
{
sw.WriteLine("A001,");
foreach (string a100 in SuccessA100)
{
sw.WriteLine(a100+",");//开始写入值
}
sw.Close();
fs1.Close();
}
}
else
{
FileStream fs = new FileStream(txtPath, FileMode.Append, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string a100 in SuccessA100)
{
sw.WriteLine(a100 + ",");//开始写入值
}
sw.Close();
fs.Close();
} }
Thread.Sleep();
}