刘磊
2025-06-09 dabbcafc629ef87d11ba55ef8cc1cdc776c047d8
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
using System;
using System.IO;
using System.Threading;
 
namespace LogLibrary.Log
{
    public static class FileUtil
    {
       
        /// <summary>
        /// 追加内容到指定文件中
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="content"></param>
        public static void WriteAppend(string filePath, string content)
        {
            WriteAppend(filePath, new string[] { content });
        }
 
        public static void WriteAppend(string filePath, string[] contents)
        {
            using (FileStream fs = new(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
            {
                fs.Seek(fs.Length, SeekOrigin.Current);
 
                string content = string.Join(Environment.NewLine, contents) + Environment.NewLine;
 
                //byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(content);
                byte[] data = System.Text.Encoding.Default.GetBytes(content);
 
 
                fs.Write(data, 0, data.Length);
 
                fs.Close();
                fs.Dispose();
            }
        }
    }
}