Skip to content

Commit 54a0ea8

Browse files
committed
FileSystem.OpenWrite fixed (use mode OpenOrCreate instead of CreateOrTruncate)
1 parent 0dfd312 commit 54a0ea8

File tree

3 files changed

+218
-201
lines changed

3 files changed

+218
-201
lines changed

Core.Common.Standard/DataAccess/FileHelper.cs

Lines changed: 163 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -6,191 +6,206 @@
66

77
// ReSharper disable UseFileSystem
88

9-
namespace KY.Core.DataAccess
9+
namespace KY.Core.DataAccess;
10+
11+
internal class FileHelper
1012
{
11-
internal class FileHelper
13+
private static readonly Regex sizeRegex = new(@"^(?<size>[0-9.,]+)\s?(?<unit>[A-z]+)$");
14+
private readonly PathHelper pathHelper;
15+
16+
public FileHelper(PathHelper pathHelper)
1217
{
13-
private static readonly Regex sizeRegex = new Regex(@"^(?<size>[0-9.,]+)\s?(?<unit>[A-z]+)$");
14-
private readonly PathHelper pathHelper;
18+
this.pathHelper = pathHelper;
19+
}
1520

16-
public FileHelper(PathHelper pathHelper)
17-
{
18-
this.pathHelper = pathHelper;
19-
}
21+
public FileInfo GetInfo(string path)
22+
{
23+
path = this.pathHelper.ToAbsolute(path);
24+
return new FileInfo(path);
25+
}
2026

21-
public FileInfo GetInfo(string path)
22-
{
23-
path = this.pathHelper.ToAbsolute(path);
24-
return new FileInfo(path);
25-
}
27+
public FileInfo[] GetInfos(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly)
28+
{
29+
path = this.pathHelper.ToAbsolute(path);
30+
DirectoryInfo directory = new(path);
31+
return searchPattern == null ? directory.GetFiles() : directory.GetFiles(searchPattern, option);
32+
}
2633

27-
public FileInfo[] GetInfos(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly)
28-
{
29-
path = this.pathHelper.ToAbsolute(path);
30-
DirectoryInfo directory = new DirectoryInfo(path);
31-
return searchPattern == null ? directory.GetFiles() : directory.GetFiles(searchPattern, option);
32-
}
34+
public string[] GetFiles(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly)
35+
{
36+
path = this.pathHelper.ToAbsolute(path);
37+
return searchPattern == null ? Directory.GetFiles(path) : Directory.GetFiles(path, searchPattern, option);
38+
}
3339

34-
public string[] GetFiles(string path, string searchPattern = null, SearchOption option = SearchOption.TopDirectoryOnly)
35-
{
36-
path = this.pathHelper.ToAbsolute(path);
37-
return searchPattern == null ? Directory.GetFiles(path) : Directory.GetFiles(path, searchPattern, option);
38-
}
40+
public string ReadAllText(string path, Encoding encoding = null)
41+
{
42+
path = this.pathHelper.ToAbsolute(path);
43+
return File.ReadAllText(path, encoding ?? Encoding.UTF8);
44+
}
3945

40-
public string ReadAllText(string path, Encoding encoding = null)
41-
{
42-
path = this.pathHelper.ToAbsolute(path);
43-
return File.ReadAllText(path, encoding ?? Encoding.UTF8);
44-
}
46+
public void WriteAllText(string path, string content, Encoding encoding = null)
47+
{
48+
path = this.pathHelper.ToAbsolute(path);
49+
File.WriteAllText(path, content, encoding ?? Encoding.UTF8);
50+
}
4551

46-
public void WriteAllText(string path, string content, Encoding encoding = null)
47-
{
48-
path = this.pathHelper.ToAbsolute(path);
49-
File.WriteAllText(path, content, encoding ?? Encoding.UTF8);
50-
}
52+
public void AppendAllText(string path, string content, Encoding encoding = null)
53+
{
54+
path = this.pathHelper.ToAbsolute(path);
55+
File.AppendAllText(path, content, encoding ?? Encoding.UTF8);
56+
}
5157

52-
public void AppendAllText(string path, string content, Encoding encoding = null)
53-
{
54-
path = this.pathHelper.ToAbsolute(path);
55-
File.AppendAllText(path, content, encoding ?? Encoding.UTF8);
56-
}
58+
public void WriteAllBytes(string path, byte[] content)
59+
{
60+
path = this.pathHelper.ToAbsolute(path);
61+
File.WriteAllBytes(path, content);
62+
}
5763

58-
public void WriteAllBytes(string path, byte[] content)
59-
{
60-
path = this.pathHelper.ToAbsolute(path);
61-
File.WriteAllBytes(path, content);
62-
}
64+
public bool Exists(params string[] pathChunks)
65+
{
66+
string path = this.pathHelper.ToAbsolute(pathChunks);
67+
return File.Exists(path);
68+
}
6369

64-
public bool Exists(params string[] pathChunks)
65-
{
66-
string path = this.pathHelper.ToAbsolute(pathChunks);
67-
return File.Exists(path);
68-
}
70+
public void Delete(params string[] pathChunks)
71+
{
72+
string path = this.pathHelper.ToAbsolute(pathChunks);
73+
File.Delete(path);
74+
}
6975

70-
public void Delete(params string[] pathChunks)
71-
{
72-
string path = this.pathHelper.ToAbsolute(pathChunks);
73-
File.Delete(path);
74-
}
76+
public string[] ReadAllLines(string path, Encoding encoding = null)
77+
{
78+
path = this.pathHelper.ToAbsolute(path);
79+
return File.ReadAllLines(path, encoding ?? Encoding.UTF8);
80+
}
7581

76-
public string[] ReadAllLines(string path, Encoding encoding = null)
77-
{
78-
path = this.pathHelper.ToAbsolute(path);
79-
return File.ReadAllLines(path, encoding ?? Encoding.UTF8);
80-
}
82+
public XElement ReadXml(params string[] pathChunks)
83+
{
84+
string path = this.pathHelper.ToAbsolute(pathChunks);
85+
return XElement.Load(path);
86+
}
8187

82-
public XElement ReadXml(params string[] pathChunks)
83-
{
84-
string path = this.pathHelper.ToAbsolute(pathChunks);
85-
return XElement.Load(path);
86-
}
88+
public Stream OpenRead(params string[] pathChunks)
89+
{
90+
string path = this.pathHelper.ToAbsolute(pathChunks);
91+
return File.OpenRead(path);
92+
}
8793

88-
public Stream OpenRead(params string[] pathChunks)
89-
{
90-
string path = this.pathHelper.ToAbsolute(pathChunks);
91-
return File.OpenRead(path);
92-
}
94+
public Stream OpenWrite(params string[] pathChunks)
95+
{
96+
string path = this.pathHelper.ToAbsolute(pathChunks);
97+
return File.Open(path, FileMode.OpenOrCreate);
98+
}
99+
100+
public void Copy(string from, string to, bool overwrite = false)
101+
{
102+
from = this.pathHelper.ToAbsolute(from);
103+
to = this.pathHelper.ToAbsolute(to);
104+
File.Copy(from, to, overwrite);
105+
}
106+
107+
public void Move(string path, string oldFileName, string newFileName, bool overwrite = false)
108+
{
109+
string from = this.pathHelper.Combine(path, oldFileName);
110+
string to = this.pathHelper.Combine(path, newFileName);
111+
this.Move(from, to, overwrite);
112+
}
93113

94-
public Stream OpenWrite(params string[] pathChunks)
114+
public void Move(string from, string to, bool overwrite = false)
115+
{
116+
from = this.pathHelper.ToAbsolute(from);
117+
to = this.pathHelper.ToAbsolute(to);
118+
if (overwrite && File.Exists(to))
95119
{
96-
string path = this.pathHelper.ToAbsolute(pathChunks);
97-
return File.OpenWrite(path);
120+
File.Delete(to);
98121
}
122+
File.Move(from, to);
123+
}
124+
125+
public void Write(string path, XElement xml)
126+
{
127+
path = this.pathHelper.ToAbsolute(path);
128+
xml.Save(path);
129+
}
99130

100-
public void Copy(string from, string to, bool overwrite = false)
131+
public string FormatSize(long size)
132+
{
133+
if (size >= 1024L * 1024 * 1024 * 1024 * 1024 * 1024)
101134
{
102-
from = this.pathHelper.ToAbsolute(from);
103-
to = this.pathHelper.ToAbsolute(to);
104-
File.Copy(from, to, overwrite);
135+
return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024 / 1024:0.#} EB";
105136
}
106-
107-
public void Move(string path, string oldFileName, string newFileName, bool overwrite = false)
137+
if (size >= 1024L * 1024 * 1024 * 1024 * 1024)
108138
{
109-
string from = this.pathHelper.Combine(path, oldFileName);
110-
string to = this.pathHelper.Combine(path, newFileName);
111-
this.Move(from, to, overwrite);
139+
return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024:0.#} PB";
112140
}
113-
114-
public void Move(string from, string to, bool overwrite = false)
141+
if (size >= 1024L * 1024 * 1024 * 1024)
115142
{
116-
from = this.pathHelper.ToAbsolute(from);
117-
to = this.pathHelper.ToAbsolute(to);
118-
if (overwrite && File.Exists(to))
119-
{
120-
File.Delete(to);
121-
}
122-
File.Move(from, to);
143+
return $"{size / 1024.0 / 1024 / 1024 / 1024:0.#} TB";
123144
}
124-
125-
public void Write(string path, XElement xml)
145+
if (size >= 1024 * 1024 * 1024)
126146
{
127-
path = this.pathHelper.ToAbsolute(path);
128-
xml.Save(path);
147+
return $"{size / 1024.0 / 1024 / 1024:0.#} GB";
129148
}
130-
131-
public string FormatSize(long size)
149+
if (size >= 1024 * 1024)
132150
{
133-
if (size >= 1024L * 1024 * 1024 * 1024 * 1024 * 1024)
134-
return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024 / 1024:0.#} EB";
135-
if (size >= 1024L * 1024 * 1024 * 1024 * 1024)
136-
return $"{size / 1024.0 / 1024 / 1024 / 1024 / 1024:0.#} PB";
137-
if (size >= 1024L * 1024 * 1024 * 1024)
138-
return $"{size / 1024.0 / 1024 / 1024 / 1024:0.#} TB";
139-
if (size >= 1024 * 1024 * 1024)
140-
return $"{size / 1024.0 / 1024 / 1024:0.#} GB";
141-
if (size >= 1024 * 1024)
142-
return $"{size / 1024.0 / 1024:0.#} MB";
143-
if (size >= 1024)
144-
return $"{size / 1024.0:0.#} kB";
145-
return $"{size} B";
151+
return $"{size / 1024.0 / 1024:0.#} MB";
146152
}
147-
148-
public long ParseSize(string value)
153+
if (size >= 1024)
149154
{
150-
if (string.IsNullOrEmpty(value))
151-
return 0;
152-
Match match = sizeRegex.Match(value);
153-
if (!match.Success)
154-
return 0;
155-
double size = double.Parse(match.Groups["size"].Value);
156-
switch (match.Groups["unit"].Value.ToUpper())
157-
{
158-
case "EB":
159-
return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
160-
case "PB":
161-
return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024);
162-
case "TB":
163-
return (long)(size * 1024 * 1024 * 1024 * 1024);
164-
case "GB":
165-
return (long)(size * 1024 * 1024 * 1024);
166-
case "MB":
167-
return (long)(size * 1024 * 1024);
168-
case "KB":
169-
return (long)(size * 1024);
170-
default:
171-
return (long)size;
172-
}
155+
return $"{size / 1024.0:0.#} kB";
173156
}
157+
return $"{size} B";
158+
}
174159

175-
public string GetName(string path)
160+
public long ParseSize(string value)
161+
{
162+
if (string.IsNullOrEmpty(value))
176163
{
177-
return Path.GetFileName(path);
164+
return 0;
178165
}
179-
180-
public FileSystemWatcher Watch(string path)
166+
Match match = sizeRegex.Match(value);
167+
if (!match.Success)
181168
{
182-
string absolutePath = FileSystem.ToAbsolutePath(path);
183-
string directory = FileSystem.GetDirectoryName(absolutePath);
184-
string file = FileSystem.GetFileName(absolutePath);
185-
FileSystemWatcher watchdog = new (directory);
186-
watchdog.Filter = file;
187-
watchdog.EnableRaisingEvents = true;
188-
return watchdog;
169+
return 0;
189170
}
190-
191-
public DateTime GetLastWriteTime(params string[] pathChunks)
171+
double size = double.Parse(match.Groups["size"].Value);
172+
switch (match.Groups["unit"].Value.ToUpper())
192173
{
193-
return File.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks));
174+
case "EB":
175+
return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
176+
case "PB":
177+
return (long)(size * 1024 * 1024 * 1024 * 1024 * 1024);
178+
case "TB":
179+
return (long)(size * 1024 * 1024 * 1024 * 1024);
180+
case "GB":
181+
return (long)(size * 1024 * 1024 * 1024);
182+
case "MB":
183+
return (long)(size * 1024 * 1024);
184+
case "KB":
185+
return (long)(size * 1024);
186+
default:
187+
return (long)size;
194188
}
195189
}
190+
191+
public string GetName(string path)
192+
{
193+
return Path.GetFileName(path);
194+
}
195+
196+
public FileSystemWatcher Watch(string path)
197+
{
198+
string absolutePath = FileSystem.ToAbsolutePath(path);
199+
string directory = FileSystem.GetDirectoryName(absolutePath);
200+
string file = FileSystem.GetFileName(absolutePath);
201+
FileSystemWatcher watchdog = new(directory);
202+
watchdog.Filter = file;
203+
watchdog.EnableRaisingEvents = true;
204+
return watchdog;
205+
}
206+
207+
public DateTime GetLastWriteTime(params string[] pathChunks)
208+
{
209+
return File.GetLastWriteTime(this.pathHelper.ToAbsolute(pathChunks));
210+
}
196211
}

Core.Common.Standard/Extension/ExpressionExtension.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public static MemberInfo ExtractMember<T>(this Expression<T> expression)
6060
if (expression == null)
6161
return null;
6262

63+
if (expression.Body is MethodCallExpression methodCallExpression)
64+
return methodCallExpression.Method;
65+
6366
MemberExpression memberExpression = ExtractMemberExpression(expression);
6467
var memberInfo = memberExpression.Member;
6568
if (memberInfo == null)
@@ -112,19 +115,17 @@ private static MemberExpression ExtractMemberExpression<T>(Expression<T> propert
112115

113116
private static MemberExpression ExtractMemberExpression(Expression expression)
114117
{
115-
var memberExpression = expression as MemberExpression;
116-
if (memberExpression != null)
117-
return memberExpression;
118-
119-
var unaryExpression = expression as UnaryExpression;
120-
if (unaryExpression != null)
121-
return ExtractMemberExpression(unaryExpression.Operand);
122-
123-
var binaryExpression = expression as BinaryExpression;
124-
if (binaryExpression != null)
125-
return ExtractMemberExpression(binaryExpression.Left);
126-
127-
throw new InvalidOperationException(/*Properties.Resources.ExceptionTextUnknownExpressionType*/);
118+
switch (expression)
119+
{
120+
case MemberExpression memberExpression:
121+
return memberExpression;
122+
case UnaryExpression unaryExpression:
123+
return ExtractMemberExpression(unaryExpression.Operand);
124+
case BinaryExpression binaryExpression:
125+
return ExtractMemberExpression(binaryExpression.Left);
126+
default:
127+
throw new InvalidOperationException( /*Properties.Resources.ExceptionTextUnknownExpressionType*/);
128+
}
128129
}
129130

130131
public static List<MemberExpression> CollectMemberExpressions<T>(this Expression<T> expression)

0 commit comments

Comments
 (0)