alielie
2026-03-02 12e606635c42ea20f4b7c2bdda5204237a4b4c96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.IO;
using System.Text;
 
namespace CSVHelper
{
    public class CSVPrinter
    {
        public static void Print(string dir, string fileName, string title, string line)
        {
            EnsureDirectoryExists(dir);
            string path = Path.Combine(dir, fileName);
            lock (path)
            {
                if (!File.Exists(path))
                {
                    using (var fs = File.Create(path))
                    using (var writer = new StreamWriter(fs, Encoding.UTF8))
                    {
                        writer.WriteLine(title);
                        writer.WriteLine(line);
                        writer.Flush();
                    }
                }
                else
                {
                    using (var fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                    using (var writer = new StreamWriter(fs, Encoding.UTF8))
                    {
                        writer.WriteLine(line);
                        writer.Flush();
                    }
                }
            }
        }
 
        private static void EnsureDirectoryExists(string logDir)
        {
            if (!Directory.Exists(logDir))
            {
                Directory.CreateDirectory(logDir);
            }
        }
    }
}