Compare commits
2 Commits
4c08ad0064
...
826c11aa55
| Author | SHA1 | Date | |
|---|---|---|---|
| 826c11aa55 | |||
| 7fccb3810f |
@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<!--package info-->
|
||||
<PackageId>DTLib</PackageId>
|
||||
<Version>1.4.1</Version>
|
||||
<Version>1.4.3</Version>
|
||||
<Authors>Timerix</Authors>
|
||||
<Description>Library for all my C# projects</Description>
|
||||
<RepositoryType>GIT</RepositoryType>
|
||||
|
||||
@ -19,6 +19,7 @@ public static class Directory
|
||||
|
||||
System.IO.Directory.CreateDirectory(dir.Str);
|
||||
}
|
||||
|
||||
/// копирует все файлы и папки
|
||||
public static void Copy(IOPath sourceDir, IOPath newDir, bool owerwrite)
|
||||
{
|
||||
@ -54,7 +55,7 @@ public static class Directory
|
||||
Delete(target_path);
|
||||
else throw new Exception($"directory {target_path} already exists");
|
||||
}
|
||||
else Directory.Create(target_path.ParentDir());
|
||||
else Create(target_path.ParentDir());
|
||||
System.IO.Directory.Move(current_path.Str, target_path.Str);
|
||||
}
|
||||
|
||||
@ -86,9 +87,9 @@ public static class Directory
|
||||
all_subdirs = new List<IOPath>();
|
||||
return GetAllFiles_internal(dir, all_subdirs);
|
||||
}
|
||||
|
||||
private static List<IOPath> GetAllFiles_internal(IOPath dir, List<IOPath>? all_subdirs)
|
||||
{
|
||||
bool rememberSubdirs = all_subdirs is not null;
|
||||
var all_files = new List<IOPath>();
|
||||
IOPath[] cur_files = GetFiles(dir);
|
||||
for (int i = 0; i < cur_files.Length; i++)
|
||||
@ -96,20 +97,12 @@ public static class Directory
|
||||
IOPath[] cur_subdirs = GetDirectories(dir);
|
||||
for (int i = 0; i < cur_subdirs.Length; i++)
|
||||
{
|
||||
if(rememberSubdirs)
|
||||
all_subdirs?.Add(cur_subdirs[i]);
|
||||
all_files.AddRange(GetAllFiles_internal(cur_subdirs[i], all_subdirs));
|
||||
}
|
||||
|
||||
return all_files;
|
||||
}
|
||||
|
||||
public static string GetCurrent() => System.IO.Directory.GetCurrentDirectory();
|
||||
|
||||
public static void CreateSymlink(string sourcePath, string symlinkPath)
|
||||
{
|
||||
if (symlinkPath.Contains(Path.Sep))
|
||||
Create(Path.ParentDir(symlinkPath));
|
||||
if (!Symlink.CreateSymbolicLink(symlinkPath, sourcePath, Symlink.SymlinkTarget.Directory))
|
||||
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkPath}, {sourcePath})");
|
||||
}
|
||||
public static IOPath GetCurrent() => new IOPath(System.IO.Directory.GetCurrentDirectory(), true);
|
||||
}
|
||||
@ -1,4 +1,7 @@
|
||||
namespace DTLib.Filesystem;
|
||||
using FileMode = System.IO.FileMode;
|
||||
using FileAccess = System.IO.FileAccess;
|
||||
|
||||
namespace DTLib.Filesystem;
|
||||
|
||||
public static class File
|
||||
{
|
||||
@ -14,23 +17,22 @@ public static class File
|
||||
|
||||
Directory.Create(file.ParentDir());
|
||||
using System.IO.FileStream stream = System.IO.File.Create(file.Str);
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public static void Copy(IOPath srcPath, IOPath newPath, bool overwrite)
|
||||
{
|
||||
if (Exists(newPath))
|
||||
{
|
||||
if(overwrite) System.IO.File.Delete(newPath.Str);
|
||||
if (overwrite)
|
||||
Delete(newPath);
|
||||
else throw new Exception($"file <{newPath}> alredy exists");
|
||||
}
|
||||
else Directory.Create(newPath.ParentDir());
|
||||
using var srcFile=System.IO.File.Open(srcPath.Str, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
|
||||
using var newFile=System.IO.File.Open(newPath.Str, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
|
||||
|
||||
using var srcFile = OpenRead(srcPath);
|
||||
using var newFile = OpenWrite(newPath);
|
||||
srcFile.CopyTo(newFile);
|
||||
srcFile.Close();
|
||||
newFile.Flush();
|
||||
newFile.Close();
|
||||
}
|
||||
|
||||
public static void Move(IOPath current_path, IOPath target_path, bool overwrite)
|
||||
@ -42,6 +44,7 @@ public static class File
|
||||
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);
|
||||
}
|
||||
|
||||
@ -49,13 +52,11 @@ public static class File
|
||||
|
||||
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");
|
||||
stream.Close();
|
||||
return output;
|
||||
}
|
||||
|
||||
@ -65,47 +66,37 @@ public static class File
|
||||
{
|
||||
using System.IO.FileStream stream = OpenWrite(file);
|
||||
stream.Write(content, 0, content.Length);
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public static void WriteAllText(IOPath file, string content) => WriteAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||
public static void WriteAllText(IOPath file, string content) =>
|
||||
WriteAllBytes(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);
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public static void AppendAllText(IOPath file, string content) => AppendAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||
public static void AppendAllText(IOPath file, string content) =>
|
||||
AppendAllBytes(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, System.IO.FileMode.Open, System.IO.FileAccess.Read,
|
||||
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)
|
||||
{
|
||||
if (Exists(file))
|
||||
Delete(file);
|
||||
Create(file);
|
||||
return System.IO.File.Open(file.Str, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Read);
|
||||
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)
|
||||
{
|
||||
|
||||
Create(file);
|
||||
return System.IO.File.Open(file.Str, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);
|
||||
}
|
||||
|
||||
public static void CreateSymlink(IOPath sourcePath, IOPath symlinkPath)
|
||||
{
|
||||
if (symlinkPath.Contains(Path.Sep))
|
||||
Directory.Create(symlinkPath.ParentDir());
|
||||
if (!Symlink.CreateSymbolicLink(symlinkPath.Str, sourcePath.Str, Symlink.SymlinkTarget.File))
|
||||
throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkPath}, {sourcePath})");
|
||||
Directory.Create(file.ParentDir());
|
||||
return System.IO.File.Open(file.Str, FileMode.Append, FileAccess.Write, System.IO.FileShare.Read);
|
||||
}
|
||||
}
|
||||
@ -40,18 +40,18 @@ public readonly struct IOPath
|
||||
return new string(fixed_path);
|
||||
}
|
||||
|
||||
public static IOPath[] ArrayCast(string[] a, bool correct_separators=false)
|
||||
public static IOPath[] ArrayCast(string[] a, bool separatorsFixed=false)
|
||||
{
|
||||
IOPath[] b = new IOPath[a.Length];
|
||||
for (int i = 0; i < a.Length; i++)
|
||||
b[i] = new IOPath(a[i], correct_separators);
|
||||
b[i] = new IOPath(a[i], separatorsFixed);
|
||||
return b;
|
||||
}
|
||||
public static IOPath[] ListCast(IList<string> a, bool correct_separators=false)
|
||||
public static IOPath[] ListCast(IList<string> a, bool separatorsFixed=false)
|
||||
{
|
||||
IOPath[] b = new IOPath[a.Count];
|
||||
for (int i = 0; i < a.Count; i++)
|
||||
b[i] = new IOPath(a[i], correct_separators);
|
||||
b[i] = new IOPath(a[i], separatorsFixed);
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
@ -25,14 +25,19 @@ public static class Path
|
||||
char c = r[i];
|
||||
switch (c)
|
||||
{
|
||||
case '\n': case '\r':
|
||||
case ':': case ';':
|
||||
case '\n':
|
||||
case '\r':
|
||||
case ':':
|
||||
case ';':
|
||||
break;
|
||||
case '/': case '\\':
|
||||
case '/':
|
||||
case '\\':
|
||||
b.Append('-');
|
||||
break;
|
||||
case '<': case '>':
|
||||
case '?': case '|':
|
||||
case '<':
|
||||
case '>':
|
||||
case '?':
|
||||
case '|':
|
||||
b.Append('_');
|
||||
break;
|
||||
case '"':
|
||||
@ -46,6 +51,7 @@ public static class Path
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new IOPath(b.ToString(), true);
|
||||
}
|
||||
|
||||
@ -71,6 +77,7 @@ public static class Path
|
||||
}
|
||||
else needSeparator[i] = false;
|
||||
}
|
||||
|
||||
lengthSum += parts[parts.Length - 1].Length;
|
||||
var buffer = new char[lengthSum];
|
||||
parts[0].Str.CopyTo(buffer, 0);
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DTLib.Filesystem;
|
||||
|
||||
internal class Symlink
|
||||
{
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
||||
internal static extern bool CreateSymbolicLink(string symlinkName, string sourceName, SymlinkTarget type);
|
||||
|
||||
internal enum SymlinkTarget
|
||||
{
|
||||
File,
|
||||
Directory
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user