forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOHelper.cs
191 lines (182 loc) · 5.73 KB
/
IOHelper.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CYQ.Data.Tool
{
internal static class IOHelper
{
private static List<object> tenObj = new List<object>(10);
private static List<object> TenObj
{
get
{
if (tenObj.Count == 0)
{
for (int i = 0; i < 10; i++)
{
tenObj.Add(new object());
}
}
return tenObj;
}
}
private static object GetLockObj(int length)
{
int i = length % 9;
return TenObj[i];
}
/// <summary>
/// 先自动识别UTF8,否则归到Default编码读取
/// </summary>
/// <returns></returns>
public static string ReadAllText(string fileName)
{
return ReadAllText(fileName, Encoding.Default);
}
public static string ReadAllText(string fileName, Encoding encoding)
{
try
{
if (!File.Exists(fileName))
{
return string.Empty;
}
Byte[] buff = null;
lock (GetLockObj(fileName.Length))
{
if (!File.Exists(fileName))//多线程情况处理
{
return string.Empty;
}
buff = File.ReadAllBytes(fileName);
}
if (buff[0] == 239 && buff[1] == 187 && buff[2] == 191)
{
return Encoding.UTF8.GetString(buff, 3, buff.Length - 3);
}
else if (buff[0] == 255 && buff[1] == 254)
{
return Encoding.Unicode.GetString(buff, 2, buff.Length - 2);
}
else if (buff[0] == 254 && buff[1] == 255)
{
if (buff.Length > 3 && buff[2] == 0 && buff[3] == 0)
{
return Encoding.UTF32.GetString(buff, 4, buff.Length - 4);
}
return Encoding.BigEndianUnicode.GetString(buff, 2, buff.Length - 2);
}
return encoding.GetString(buff);
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
return string.Empty;
}
public static bool Write(string fileName, string text)
{
return Save(fileName, text, false, Encoding.Default, true);
}
public static bool Write(string fileName, string text, Encoding encode)
{
return Save(fileName, text, false, encode, true);
}
public static bool Append(string fileName, string text)
{
return Save(fileName, text, true, true);
}
internal static bool Save(string fileName, string text, bool isAppend, bool writeLogOnError)
{
return Save(fileName, text, true, Encoding.Default, writeLogOnError);
}
internal static bool Save(string fileName, string text, bool isAppend, Encoding encode, bool writeLogOnError)
{
try
{
string folder = Path.GetDirectoryName(fileName);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
lock (GetLockObj(fileName.Length))
{
using (StreamWriter writer = new StreamWriter(fileName, isAppend, encode))
{
writer.Write(text);
}
}
return true;
}
catch (Exception err)
{
if (writeLogOnError)
{
Log.WriteLogToTxt(err);
}
else
{
Error.Throw("IOHelper.Save() : " + err.Message);
}
}
return false;
}
internal static bool Delete(string fileName)
{
try
{
if (File.Exists(fileName))
{
lock (GetLockObj(fileName.Length))
{
if (File.Exists(fileName))
{
File.Delete(fileName);
return true;
}
}
}
}
catch
{
}
return false;
}
public static bool IsLastFileWriteTimeChanged(string fileName, ref DateTime compareTimeUtc)
{
bool isChanged = false;
IOInfo info = new IOInfo(fileName);
if (info.Exists && info.LastWriteTimeUtc != compareTimeUtc)
{
isChanged = true;
compareTimeUtc = info.LastWriteTimeUtc;
}
return isChanged;
}
}
internal class IOInfo : FileSystemInfo
{
public IOInfo(string fileName)
{
base.FullPath = fileName;
}
public override void Delete()
{
}
public override bool Exists
{
get
{
return File.Exists(base.FullPath);
}
}
public override string Name
{
get
{
return null;
}
}
}
}