-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathFiles.cs
133 lines (121 loc) · 3.58 KB
/
Files.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
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Waher.Runtime.IO
{
/// <summary>
/// Contains static methods
/// </summary>
public static class Files
{
/// <summary>
/// Reads a binary file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <returns>Binary content.</returns>
public static async Task<byte[]> ReadAllBytesAsync(string FileName)
{
using (FileStream fs = File.OpenRead(FileName))
{
return await fs.ReadAllAsync();
}
}
/// <summary>
/// Creates a binary file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <param name="Data">Binary data</param>
public static Task WriteAllBytesAsync(string FileName, byte[] Data)
{
return WriteAllBytesAsync(FileName, Data, 0, Data.Length);
}
/// <summary>
/// Creates a binary file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <param name="Data">Binary data</param>
/// <param name="Offset">Offset into <paramref name="Data"/> to start.</param>
/// <param name="Length">Number of bytes to write.</param>
public static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
{
using (FileStream fs = File.Create(FileName))
{
await fs.WriteAsync(Data, Offset, Length);
}
}
/// <summary>
/// Appends a binary file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <param name="Data">Binary data</param>
public static Task AppendAllBytesAsync(string FileName, byte[] Data)
{
return AppendAllBytesAsync(FileName, Data, 0, Data.Length);
}
/// <summary>
/// Appends a binary file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <param name="Data">Binary data</param>
/// <param name="Offset">Offset into <paramref name="Data"/> to start.</param>
/// <param name="Length">Number of bytes to write.</param>
public static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
{
using (FileStream fs = File.OpenWrite(FileName))
{
fs.Position = fs.Length;
await fs.WriteAsync(Data, Offset, Length);
}
}
/// <summary>
/// Reads a text file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <returns>Decoded text.</returns>
public static async Task<string> ReadAllTextAsync(string FileName)
{
byte[] Bin = await ReadAllBytesAsync(FileName);
return Strings.GetString(Bin, Encoding.UTF8);
}
/// <summary>
/// Creates a text file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <param name="Text">Text</param>
public static Task WriteAllTextAsync(string FileName, string Text)
{
return WriteAllTextAsync(FileName, Text, Encoding.UTF8);
}
/// <summary>
/// Creates a text file asynchronously.
/// </summary>
/// <param name="FileName">Filename.</param>
/// <param name="Text">Text</param>
/// <param name="Encoding">Encoding to use</param>
public static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
{
using (FileStream fs = File.Create(FileName))
{
byte[] Preamble = Encoding.GetPreamble();
byte[] Data = Encoding.GetBytes(Text);
int i, c = Preamble.Length;
if (c > 0)
{
if (c > Data.Length)
i = 0;
else
{
for (i = 0; i < c; i++)
{
if (Preamble[i] != Data[i])
break;
}
}
if (i < c)
await fs.WriteAsync(Preamble, 0, c);
}
await fs.WriteAsync(Data, 0, Data.Length);
}
}
}
}