-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFileInfoExtensions.cs
More file actions
207 lines (190 loc) · 6.78 KB
/
Copy pathFileInfoExtensions.cs
File metadata and controls
207 lines (190 loc) · 6.78 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="FileInfo" /> objects.
/// </summary>
public static class FileInfoExtensions
{
extension(FileInfo file)
{
/// <summary>
/// Opens the file, reads the contents of the file into a <see cref="byte" />[], and then closes the file.
/// </summary>
/// <returns>
/// A new <see cref="byte" />[] containing the contents of the file.
/// </returns>
public byte[] ReadAllBytes()
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadAllBytes(file.FullName);
}
/// <summary>
/// Opens the file, reads all lines of the file into a <see cref="string" />[], and then closes the file.
/// </summary>
/// <returns>
/// A new <see cref="string" />[] containing all lines of the file.
/// </returns>
public string[] ReadAllLines()
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadAllLines(file.FullName);
}
/// <summary>
/// Opens the file, reads all lines of the file with the specified encoding into a <see cref="string" />[], and then closes the file.
/// </summary>
/// <param name="encoding">The encoding applied to the contents of the file.</param>
/// <returns>
/// A new <see cref="string" />[] containing all lines of the file.
/// </returns>
public string[] ReadAllLines(Encoding encoding)
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadAllLines(file.FullName, encoding);
}
/// <summary>
/// Opens a text file, reads all lines of the file into a <see cref="string" />, and then closes the file.
/// </summary>
/// <returns>
/// A <see cref="string" /> containing all lines of the file.
/// </returns>
public string ReadAllText()
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadAllText(file.FullName);
}
/// <summary>
/// Opens a text file, reads all lines of the file with the specified encoding into a <see cref="string" />, and then closes the file.
/// </summary>
/// <param name="encoding">The encoding applied to the contents of the file.</param>
/// <returns>
/// A <see cref="string" /> containing all lines of the file.
/// </returns>
public string ReadAllText(Encoding encoding)
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadAllText(file.FullName, encoding);
}
/// <summary>
/// Reads the lines of a file.
/// </summary>
/// <returns>
/// All the lines of the file, or the lines that are the result of a query.
/// </returns>
public IEnumerable<string> ReadLines()
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadLines(file.FullName);
}
/// <summary>
/// Reads the lines of a file that has a specified encoding.
/// </summary>
/// <param name="encoding">The encoding applied to the contents of the file.</param>
/// <returns>
/// All the lines of the file, or the lines that are the result of a query.
/// </returns>
public IEnumerable<string> ReadLines(Encoding encoding)
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return File.ReadLines(file.FullName, encoding);
}
/// <summary>
/// Deletes the :Zone.Identifier alternate data stream for this file.
/// </summary>
/// <returns>
/// <see langword="true" />, if the :Zone.Identifier alternate data stream was present and could be deleted;
/// otherwise, <see langword="false" />.
/// </returns>
[SupportedOSPlatform("windows")]
public bool Unblock()
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
return Native.DeleteFile($"{file.FullName}:Zone.Identifier");
}
/// <summary>
/// Compares the contents of two files. Returns <see langword="true" />, if both files are of equal size and equal binary content.
/// </summary>
/// <param name="other">The other <see cref="FileInfo" /> to compare to this <see cref="FileInfo" />.</param>
/// <returns>
/// <see langword="true" />, if both files are of equal size and equal binary content;
/// otherwise, <see langword="false" />.
/// </returns>
public bool CompareContents(FileInfo other)
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
Check.ArgumentNull(other);
Check.FileNotFound(other.FullName);
FileInfo a = new(file.FullName);
FileInfo b = new(other.FullName);
if (a.Length == b.Length)
{
using FileStream streamA = a.OpenRead();
using FileStream streamB = b.OpenRead();
byte[] bufferA = new byte[4096];
byte[] bufferB = new byte[4096];
int bytesReadA;
int bytesReadB;
do
{
bytesReadA = streamA.Read(bufferA);
bytesReadB = streamB.Read(bufferB);
if (bytesReadA != bytesReadB || !bufferA.Compare(bufferB)) return false;
}
while (bytesReadA > 0 || bytesReadB > 0);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Searches the file for the first occurrence of <paramref name="sequence" />. If not found, returns -1.
/// </summary>
/// <param name="sequence">The <see cref="byte" />[] to search for.</param>
/// <returns>
/// The index of the first occurrence of <paramref name="sequence" /> and -1, if not found.
/// </returns>
public long FindSequence(byte[] sequence)
{
return file.FindSequence(sequence, 0);
}
/// <summary>
/// Searches the file for the first occurrence of <paramref name="sequence" /> starting from <paramref name="startIndex" />. If not found, returns -1.
/// </summary>
/// <param name="sequence">The <see cref="byte" />[] to search for.</param>
/// <param name="startIndex">The zero-based starting position to start searching from.</param>
/// <returns>
/// The index of the first occurrence of <paramref name="sequence" /> and -1, if not found.
/// </returns>
public long FindSequence(byte[] sequence, int startIndex)
{
Check.ArgumentNull(file);
Check.FileNotFound(file.FullName);
Check.ArgumentNull(sequence);
Check.ArgumentEx.ArrayElementsRequired(sequence);
Check.ArgumentOutOfRangeEx.GreaterEqual0(startIndex);
using FileStream stream = file.OpenRead();
stream.Seek(startIndex, SeekOrigin.Begin);
long index = stream.FindSequence(sequence);
return index == -1 ? -1 : index + startIndex;
}
}
}
[SupportedOSPlatform("windows")]
file static class Native
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteFile(string name);
}