automatic path separator correction
This commit is contained in:
parent
ac1b8ad4f1
commit
1ab9a50019
@ -2,11 +2,12 @@
|
|||||||
|
|
||||||
public static class Directory
|
public static class Directory
|
||||||
{
|
{
|
||||||
public static bool Exists(string dir) => System.IO.Directory.Exists(dir);
|
public static bool Exists(string dir) => System.IO.Directory.Exists(Path.FixSeparators(dir));
|
||||||
|
|
||||||
/// создает папку, если её не существует
|
/// создает папку, если её не существует
|
||||||
public static void Create(string dir)
|
public static void Create(string dir)
|
||||||
{
|
{
|
||||||
|
dir = Path.FixSeparators(dir);
|
||||||
if (!Exists(dir))
|
if (!Exists(dir))
|
||||||
{
|
{
|
||||||
// проверяет существование папки, в которой нужно создать dir
|
// проверяет существование папки, в которой нужно создать dir
|
||||||
@ -46,26 +47,11 @@ public static class Directory
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// удаляет папку со всеми подпапками и файлами
|
/// удаляет папку со всеми подпапками и файлами
|
||||||
public static void Delete(string dir)
|
public static void Delete(string dir) => System.IO.Directory.Delete(Path.FixSeparators(dir), true);
|
||||||
{
|
|
||||||
var subdirs = new List<string>();
|
|
||||||
List<string> files = GetAllFiles(dir, ref subdirs);
|
|
||||||
for (int i = 0; i < files.Count; i++)
|
|
||||||
File.Delete(files[i]);
|
|
||||||
for (int i = subdirs.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
Log($"deleting {subdirs[i]}");
|
|
||||||
if (Exists(subdirs[i]))
|
|
||||||
System.IO.Directory.Delete(subdirs[i], true);
|
|
||||||
}
|
|
||||||
Log($"deleting {dir}");
|
|
||||||
if (Exists(dir))
|
|
||||||
System.IO.Directory.Delete(dir, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string[] GetFiles(string dir) => System.IO.Directory.GetFiles(dir);
|
public static string[] GetFiles(string dir) => System.IO.Directory.GetFiles(Path.FixSeparators(dir));
|
||||||
public static string[] GetFiles(string dir, string searchPattern) => System.IO.Directory.GetFiles(dir, searchPattern);
|
public static string[] GetFiles(string dir, string searchPattern) => System.IO.Directory.GetFiles(Path.FixSeparators(dir), searchPattern);
|
||||||
public static string[] GetDirectories(string dir) => System.IO.Directory.GetDirectories(dir);
|
public static string[] GetDirectories(string dir) => System.IO.Directory.GetDirectories(Path.FixSeparators(dir));
|
||||||
|
|
||||||
/// выдает список всех файлов
|
/// выдает список всех файлов
|
||||||
public static List<string> GetAllFiles(string dir)
|
public static List<string> GetAllFiles(string dir)
|
||||||
@ -100,21 +86,11 @@ public static class Directory
|
|||||||
|
|
||||||
public static void CreateSymlink(string sourceName, string symlinkName)
|
public static void CreateSymlink(string sourceName, string symlinkName)
|
||||||
{
|
{
|
||||||
|
sourceName = Path.FixSeparators(sourceName);
|
||||||
|
symlinkName = Path.FixSeparators(symlinkName);
|
||||||
if (symlinkName.Contains(Path.Sep))
|
if (symlinkName.Contains(Path.Sep))
|
||||||
Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep)));
|
Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep)));
|
||||||
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
|
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
|
||||||
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})");
|
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// copies directory with symlinks instead of files
|
|
||||||
public static int SymCopy(string srcdir, string newdir)
|
|
||||||
{
|
|
||||||
List<string> files = GetAllFiles(srcdir);
|
|
||||||
if (!srcdir.EndsWith(Path.Sep)) srcdir += Path.Sep;
|
|
||||||
if (!newdir.EndsWith(Path.Sep)) newdir += Path.Sep;
|
|
||||||
int i = 0;
|
|
||||||
for (; i < files.Count; i++)
|
|
||||||
File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir));
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,13 +4,14 @@ namespace DTLib.Filesystem;
|
|||||||
public static class File
|
public static class File
|
||||||
{
|
{
|
||||||
/// возвращает размер файла в байтах
|
/// возвращает размер файла в байтах
|
||||||
public static long GetSize(string file) => new System.IO.FileInfo(file).Length;
|
public static long GetSize(string file) => new System.IO.FileInfo(Path.FixSeparators(file)).Length;
|
||||||
|
|
||||||
public static bool Exists(string file) => System.IO.File.Exists(file);
|
public static bool Exists(string file) => System.IO.File.Exists(Path.FixSeparators(file));
|
||||||
|
|
||||||
/// если файл не существует, создаёт файл с папками из его пути и закрывает этот фвйл
|
/// если файл не существует, создаёт файл с папками из его пути и закрывает этот фвйл
|
||||||
public static void Create(string file)
|
public static void Create(string file)
|
||||||
{
|
{
|
||||||
|
file = Path.FixSeparators(file);
|
||||||
if (!Exists(file))
|
if (!Exists(file))
|
||||||
{
|
{
|
||||||
if (file.Contains(Path.Sep))
|
if (file.Contains(Path.Sep))
|
||||||
@ -20,18 +21,20 @@ public static class File
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Copy(string srcPath, string newPath, bool replace = false)
|
public static void Copy(string srcPath, string newPath, bool overwrite = false)
|
||||||
{
|
{
|
||||||
if (!replace && Exists(newPath))
|
srcPath = Path.FixSeparators(srcPath);
|
||||||
|
newPath = Path.FixSeparators(newPath);
|
||||||
|
if (!overwrite && Exists(newPath))
|
||||||
throw new Exception($"file <{newPath}> alredy exists");
|
throw new Exception($"file <{newPath}> alredy exists");
|
||||||
Create(newPath);
|
System.IO.File.Copy(srcPath, newPath, overwrite);
|
||||||
WriteAllBytes(newPath, ReadAllBytes(srcPath));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Delete(string file) => System.IO.File.Delete(file);
|
public static void Delete(string file) => System.IO.File.Delete(Path.FixSeparators(file));
|
||||||
|
|
||||||
public static byte[] ReadAllBytes(string file)
|
public static byte[] ReadAllBytes(string file)
|
||||||
{
|
{
|
||||||
|
file = Path.FixSeparators(file);
|
||||||
using System.IO.FileStream stream = OpenRead(file);
|
using System.IO.FileStream stream = OpenRead(file);
|
||||||
int size = GetSize(file).ToInt();
|
int size = GetSize(file).ToInt();
|
||||||
byte[] output = new byte[size];
|
byte[] output = new byte[size];
|
||||||
@ -45,6 +48,7 @@ public static class File
|
|||||||
|
|
||||||
public static void WriteAllBytes(string file, byte[] content)
|
public static void WriteAllBytes(string file, byte[] content)
|
||||||
{
|
{
|
||||||
|
file = Path.FixSeparators(file);
|
||||||
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);
|
||||||
stream.Close();
|
stream.Close();
|
||||||
@ -54,6 +58,7 @@ public static class File
|
|||||||
|
|
||||||
public static void AppendAllBytes(string file, byte[] content)
|
public static void AppendAllBytes(string file, byte[] content)
|
||||||
{
|
{
|
||||||
|
file = Path.FixSeparators(file);
|
||||||
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);
|
||||||
stream.Close();
|
stream.Close();
|
||||||
@ -61,12 +66,18 @@ public static class File
|
|||||||
|
|
||||||
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||||
|
|
||||||
public static System.IO.FileStream OpenRead(string file) =>
|
public static System.IO.FileStream OpenRead(string file)
|
||||||
Exists(file)
|
{
|
||||||
? System.IO.File.Open(file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)
|
file = Path.FixSeparators(file);
|
||||||
: throw new Exception($"file not found: <{file}>");
|
if (Exists(file))
|
||||||
|
return System.IO.File.Open(file, System.IO.FileMode.Open, System.IO.FileAccess.Read,
|
||||||
|
System.IO.FileShare.ReadWrite);
|
||||||
|
throw new Exception($"file not found: <{file}>");
|
||||||
|
}
|
||||||
|
|
||||||
public static System.IO.FileStream OpenWrite(string file)
|
public static System.IO.FileStream OpenWrite(string file)
|
||||||
{
|
{
|
||||||
|
file = Path.FixSeparators(file);
|
||||||
if (Exists(file))
|
if (Exists(file))
|
||||||
Delete(file);
|
Delete(file);
|
||||||
Create(file);
|
Create(file);
|
||||||
@ -74,12 +85,15 @@ public static class File
|
|||||||
}
|
}
|
||||||
public static System.IO.FileStream OpenAppend(string file)
|
public static System.IO.FileStream OpenAppend(string file)
|
||||||
{
|
{
|
||||||
|
file = Path.FixSeparators(file);
|
||||||
Create(file);
|
Create(file);
|
||||||
return System.IO.File.Open(file, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
|
return System.IO.File.Open(file, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void CreateSymlink(string sourceName, string symlinkName)
|
public static void CreateSymlink(string sourceName, string symlinkName)
|
||||||
{
|
{
|
||||||
|
sourceName = Path.FixSeparators(sourceName);
|
||||||
|
symlinkName = Path.FixSeparators(symlinkName);
|
||||||
if (symlinkName.Contains(Path.Sep))
|
if (symlinkName.Contains(Path.Sep))
|
||||||
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep)));
|
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep)));
|
||||||
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
|
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user