async methods in File
This commit is contained in:
parent
f2cdfc86b7
commit
cfa26ce807
@ -60,16 +60,37 @@ public static class File
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ReadAllText(IOPath file) => ReadAllBytes(file).BytesToString(StringConverter.UTF8);
|
|
||||||
|
public static async Task<byte[]> ReadAllBytesAsync(IOPath file)
|
||||||
|
{
|
||||||
|
using System.IO.FileStream stream = OpenRead(file);
|
||||||
|
int size = GetSize(file).ToInt();
|
||||||
|
byte[] output = new byte[size];
|
||||||
|
if (await stream.ReadAsync(output, 0, size).ConfigureAwait(false) < size)
|
||||||
|
throw new Exception("can't read all bytes");
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ReadAllText(IOPath file) =>
|
||||||
|
ReadAllBytes(file).BytesToString(StringConverter.UTF8);
|
||||||
|
public static async Task<string> ReadAllTextAsync(IOPath file) =>
|
||||||
|
(await ReadAllBytesAsync(file)).BytesToString(StringConverter.UTF8);
|
||||||
|
|
||||||
public static void WriteAllBytes(IOPath file, byte[] content)
|
public static void WriteAllBytes(IOPath file, byte[] content)
|
||||||
{
|
{
|
||||||
using System.IO.FileStream stream = OpenWrite(file);
|
using System.IO.FileStream stream = OpenWrite(file);
|
||||||
stream.Write(content, 0, content.Length);
|
stream.Write(content, 0, content.Length);
|
||||||
}
|
}
|
||||||
|
public static async Task WriteAllBytesAsync(IOPath file, byte[] content)
|
||||||
|
{
|
||||||
|
using System.IO.FileStream stream = OpenWrite(file);
|
||||||
|
await stream.WriteAsync(content, 0, content.Length);
|
||||||
|
}
|
||||||
|
|
||||||
public static void WriteAllText(IOPath file, string content) =>
|
public static async Task WriteAllText(IOPath file, string content) =>
|
||||||
WriteAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
await WriteAllBytesAsync(file, content.ToBytes(StringConverter.UTF8));
|
||||||
|
public static async Task WriteAllTextAsync(IOPath file, string content) =>
|
||||||
|
await WriteAllBytesAsync(file, content.ToBytes(StringConverter.UTF8));
|
||||||
|
|
||||||
public static void AppendAllBytes(IOPath file, byte[] content)
|
public static void AppendAllBytes(IOPath file, byte[] content)
|
||||||
{
|
{
|
||||||
@ -77,9 +98,18 @@ public static class File
|
|||||||
stream.Write(content, 0, content.Length);
|
stream.Write(content, 0, content.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task AppendAllBytesAsync(IOPath file, byte[] content)
|
||||||
|
{
|
||||||
|
using System.IO.FileStream stream = OpenAppend(file);
|
||||||
|
await stream.WriteAsync(content, 0, content.Length);
|
||||||
|
}
|
||||||
|
|
||||||
public static void AppendAllText(IOPath file, string content) =>
|
public static void AppendAllText(IOPath file, string content) =>
|
||||||
AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||||
|
|
||||||
|
public static async Task AppendAllTextAsync(IOPath file, string content) =>
|
||||||
|
await AppendAllBytesAsync(file, content.ToBytes(StringConverter.UTF8));
|
||||||
|
|
||||||
public static System.IO.FileStream OpenRead(IOPath file)
|
public static System.IO.FileStream OpenRead(IOPath file)
|
||||||
{
|
{
|
||||||
if (!Exists(file))
|
if (!Exists(file))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user