132 lines
4.9 KiB
C#
132 lines
4.9 KiB
C#
using FileMode = System.IO.FileMode;
|
||
using FileAccess = System.IO.FileAccess;
|
||
|
||
namespace DTLib.Filesystem;
|
||
|
||
public static class File
|
||
{
|
||
/// возвращает размер файла в байтах
|
||
public static long GetSize(IOPath file) => new System.IO.FileInfo(file.Str).Length;
|
||
|
||
public static bool Exists(IOPath file) => System.IO.File.Exists(file.Str);
|
||
|
||
/// если файл не существует, создаёт файл с папками из его пути и закрывает этот фвйл
|
||
public static void Create(IOPath file)
|
||
{
|
||
if (Exists(file)) return;
|
||
|
||
Directory.Create(file.ParentDir());
|
||
using System.IO.FileStream stream = System.IO.File.Create(file.Str);
|
||
}
|
||
|
||
public static void Copy(IOPath srcPath, IOPath newPath, bool overwrite)
|
||
{
|
||
if (Exists(newPath))
|
||
{
|
||
if (overwrite)
|
||
Delete(newPath);
|
||
else throw new Exception($"file <{newPath}> alredy exists");
|
||
}
|
||
else Directory.Create(newPath.ParentDir());
|
||
|
||
using var srcFile = OpenRead(srcPath);
|
||
using var newFile = OpenWrite(newPath);
|
||
srcFile.CopyTo(newFile);
|
||
newFile.Flush();
|
||
}
|
||
|
||
public static void Move(IOPath current_path, IOPath target_path, bool overwrite)
|
||
{
|
||
if (Exists(target_path))
|
||
{
|
||
if (overwrite)
|
||
Delete(target_path);
|
||
else throw new Exception($"file {target_path} already exists");
|
||
}
|
||
else Directory.Create(target_path.ParentDir());
|
||
|
||
System.IO.File.Move(current_path.Str, target_path.Str);
|
||
}
|
||
|
||
public static void Delete(IOPath file) => System.IO.File.Delete(file.Str);
|
||
|
||
public static byte[] ReadAllBytes(IOPath file)
|
||
{
|
||
using System.IO.FileStream stream = OpenRead(file);
|
||
int size = GetSize(file).ToInt();
|
||
byte[] output = new byte[size];
|
||
if (stream.Read(output, 0, size) < size)
|
||
throw new Exception("can't read all bytes");
|
||
return output;
|
||
}
|
||
|
||
|
||
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)
|
||
{
|
||
using System.IO.FileStream stream = OpenWrite(file);
|
||
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) =>
|
||
WriteAllBytes(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)
|
||
{
|
||
using System.IO.FileStream stream = OpenAppend(file);
|
||
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) =>
|
||
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)
|
||
{
|
||
if (!Exists(file))
|
||
throw new Exception($"file not found: <{file}>");
|
||
return System.IO.File.Open(file.Str, FileMode.Open, FileAccess.Read,
|
||
System.IO.FileShare.ReadWrite | System.IO.FileShare.Delete);
|
||
}
|
||
|
||
public static System.IO.FileStream OpenWrite(IOPath file)
|
||
{
|
||
Directory.Create(file.ParentDir());
|
||
return System.IO.File.Open(file.Str, FileMode.Create, FileAccess.Write, System.IO.FileShare.Read);
|
||
}
|
||
|
||
public static System.IO.FileStream OpenAppend(IOPath file)
|
||
{
|
||
Directory.Create(file.ParentDir());
|
||
return System.IO.File.Open(file.Str, FileMode.Append, FileAccess.Write, System.IO.FileShare.Read);
|
||
}
|
||
} |