Compare commits

..

2 Commits

Author SHA1 Message Date
4da75bfa8a v1.6.2 2025-03-22 20:55:10 +05:00
cfa26ce807 async methods in File 2025-03-22 20:53:16 +05:00
2 changed files with 34 additions and 4 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<!--package info--> <!--package info-->
<PackageId>DTLib</PackageId> <PackageId>DTLib</PackageId>
<Version>1.6.1</Version> <Version>1.6.2</Version>
<Authors>Timerix</Authors> <Authors>Timerix</Authors>
<Description>Library for all my C# projects</Description> <Description>Library for all my C# projects</Description>
<RepositoryType>GIT</RepositoryType> <RepositoryType>GIT</RepositoryType>

View File

@ -60,25 +60,55 @@ 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)
{ {
using System.IO.FileStream stream = OpenAppend(file); using System.IO.FileStream stream = OpenAppend(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)
{ {