Compare commits
No commits in common. "826c11aa55da3194c1a42e6c7a9c2f7f2774be9f" and "4c08ad0064c89b86990fc1ce278bb856e44e6371" have entirely different histories.
826c11aa55
...
4c08ad0064
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib</PackageId>
|
<PackageId>DTLib</PackageId>
|
||||||
<Version>1.4.3</Version>
|
<Version>1.4.1</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>
|
||||||
|
|||||||
@ -8,18 +8,17 @@ public static class Directory
|
|||||||
public static void Create(IOPath dir)
|
public static void Create(IOPath dir)
|
||||||
{
|
{
|
||||||
if (Exists(dir)) return;
|
if (Exists(dir)) return;
|
||||||
|
|
||||||
// creation of parent directories
|
// creation of parent directories
|
||||||
if (dir.Contains(Path.Sep))
|
if (dir.Contains(Path.Sep))
|
||||||
{
|
{
|
||||||
var parentDir = dir.ParentDir();
|
var parentDir = dir.ParentDir();
|
||||||
if (!Exists(parentDir))
|
if(!Exists(parentDir))
|
||||||
Create(parentDir);
|
Create(parentDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.IO.Directory.CreateDirectory(dir.Str);
|
System.IO.Directory.CreateDirectory(dir.Str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// копирует все файлы и папки
|
/// копирует все файлы и папки
|
||||||
public static void Copy(IOPath sourceDir, IOPath newDir, bool owerwrite)
|
public static void Copy(IOPath sourceDir, IOPath newDir, bool owerwrite)
|
||||||
{
|
{
|
||||||
@ -55,24 +54,24 @@ public static class Directory
|
|||||||
Delete(target_path);
|
Delete(target_path);
|
||||||
else throw new Exception($"directory {target_path} already exists");
|
else throw new Exception($"directory {target_path} already exists");
|
||||||
}
|
}
|
||||||
else Create(target_path.ParentDir());
|
else Directory.Create(target_path.ParentDir());
|
||||||
System.IO.Directory.Move(current_path.Str, target_path.Str);
|
System.IO.Directory.Move(current_path.Str, target_path.Str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// удаляет папку со всеми подпапками и файлами
|
/// удаляет папку со всеми подпапками и файлами
|
||||||
public static void Delete(IOPath dir) =>
|
public static void Delete(IOPath dir) =>
|
||||||
System.IO.Directory.Delete(dir.Str, true);
|
System.IO.Directory.Delete(dir.Str, true);
|
||||||
|
|
||||||
public static IOPath[] GetFiles(IOPath dir) =>
|
public static IOPath[] GetFiles(IOPath dir) =>
|
||||||
IOPath.ArrayCast(System.IO.Directory.GetFiles(dir.Str), true);
|
IOPath.ArrayCast(System.IO.Directory.GetFiles(dir.Str), true);
|
||||||
|
|
||||||
public static IOPath[] GetFiles(IOPath dir, string searchPattern) =>
|
public static IOPath[] GetFiles(IOPath dir, string searchPattern) =>
|
||||||
IOPath.ArrayCast(System.IO.Directory.GetFiles(dir.Str, searchPattern), true);
|
IOPath.ArrayCast(System.IO.Directory.GetFiles(dir.Str, searchPattern), true);
|
||||||
|
|
||||||
public static IOPath[] GetDirectories(IOPath dir) =>
|
public static IOPath[] GetDirectories(IOPath dir) =>
|
||||||
IOPath.ArrayCast(System.IO.Directory.GetDirectories(dir.Str), true);
|
IOPath.ArrayCast(System.IO.Directory.GetDirectories(dir.Str), true);
|
||||||
|
|
||||||
public static IOPath[] GetDirectories(IOPath dir, string searchPattern) =>
|
public static IOPath[] GetDirectories(IOPath dir, string searchPattern) =>
|
||||||
IOPath.ArrayCast(System.IO.Directory.GetDirectories(dir.Str, searchPattern), true);
|
IOPath.ArrayCast(System.IO.Directory.GetDirectories(dir.Str, searchPattern), true);
|
||||||
|
|
||||||
/// выдает список всех файлов
|
/// выдает список всех файлов
|
||||||
@ -87,9 +86,9 @@ public static class Directory
|
|||||||
all_subdirs = new List<IOPath>();
|
all_subdirs = new List<IOPath>();
|
||||||
return GetAllFiles_internal(dir, all_subdirs);
|
return GetAllFiles_internal(dir, all_subdirs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<IOPath> GetAllFiles_internal(IOPath dir, List<IOPath>? 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>();
|
var all_files = new List<IOPath>();
|
||||||
IOPath[] cur_files = GetFiles(dir);
|
IOPath[] cur_files = GetFiles(dir);
|
||||||
for (int i = 0; i < cur_files.Length; i++)
|
for (int i = 0; i < cur_files.Length; i++)
|
||||||
@ -97,12 +96,20 @@ public static class Directory
|
|||||||
IOPath[] cur_subdirs = GetDirectories(dir);
|
IOPath[] cur_subdirs = GetDirectories(dir);
|
||||||
for (int i = 0; i < cur_subdirs.Length; i++)
|
for (int i = 0; i < cur_subdirs.Length; i++)
|
||||||
{
|
{
|
||||||
all_subdirs?.Add(cur_subdirs[i]);
|
if(rememberSubdirs)
|
||||||
|
all_subdirs?.Add(cur_subdirs[i]);
|
||||||
all_files.AddRange(GetAllFiles_internal(cur_subdirs[i], all_subdirs));
|
all_files.AddRange(GetAllFiles_internal(cur_subdirs[i], all_subdirs));
|
||||||
}
|
}
|
||||||
|
|
||||||
return all_files;
|
return all_files;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IOPath GetCurrent() => new IOPath(System.IO.Directory.GetCurrentDirectory(), true);
|
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})");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,7 +1,4 @@
|
|||||||
using FileMode = System.IO.FileMode;
|
namespace DTLib.Filesystem;
|
||||||
using FileAccess = System.IO.FileAccess;
|
|
||||||
|
|
||||||
namespace DTLib.Filesystem;
|
|
||||||
|
|
||||||
public static class File
|
public static class File
|
||||||
{
|
{
|
||||||
@ -14,25 +11,26 @@ public static class File
|
|||||||
public static void Create(IOPath file)
|
public static void Create(IOPath file)
|
||||||
{
|
{
|
||||||
if (Exists(file)) return;
|
if (Exists(file)) return;
|
||||||
|
|
||||||
Directory.Create(file.ParentDir());
|
Directory.Create(file.ParentDir());
|
||||||
using System.IO.FileStream stream = System.IO.File.Create(file.Str);
|
using System.IO.FileStream stream = System.IO.File.Create(file.Str);
|
||||||
|
stream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Copy(IOPath srcPath, IOPath newPath, bool overwrite)
|
public static void Copy(IOPath srcPath, IOPath newPath, bool overwrite)
|
||||||
{
|
{
|
||||||
if (Exists(newPath))
|
if (Exists(newPath))
|
||||||
{
|
{
|
||||||
if (overwrite)
|
if(overwrite) System.IO.File.Delete(newPath.Str);
|
||||||
Delete(newPath);
|
|
||||||
else throw new Exception($"file <{newPath}> alredy exists");
|
else throw new Exception($"file <{newPath}> alredy exists");
|
||||||
}
|
}
|
||||||
else Directory.Create(newPath.ParentDir());
|
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 srcFile = OpenRead(srcPath);
|
using var newFile=System.IO.File.Open(newPath.Str, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
|
||||||
using var newFile = OpenWrite(newPath);
|
|
||||||
srcFile.CopyTo(newFile);
|
srcFile.CopyTo(newFile);
|
||||||
|
srcFile.Close();
|
||||||
newFile.Flush();
|
newFile.Flush();
|
||||||
|
newFile.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Move(IOPath current_path, IOPath target_path, bool overwrite)
|
public static void Move(IOPath current_path, IOPath target_path, bool overwrite)
|
||||||
@ -44,7 +42,6 @@ public static class File
|
|||||||
else throw new Exception($"file {target_path} already exists");
|
else throw new Exception($"file {target_path} already exists");
|
||||||
}
|
}
|
||||||
else Directory.Create(target_path.ParentDir());
|
else Directory.Create(target_path.ParentDir());
|
||||||
|
|
||||||
System.IO.File.Move(current_path.Str, target_path.Str);
|
System.IO.File.Move(current_path.Str, target_path.Str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,11 +49,13 @@ public static class File
|
|||||||
|
|
||||||
public static byte[] ReadAllBytes(IOPath file)
|
public static byte[] ReadAllBytes(IOPath 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];
|
||||||
if (stream.Read(output, 0, size) < size)
|
if (stream.Read(output, 0, size) < size)
|
||||||
throw new Exception("can't read all bytes");
|
throw new Exception("can't read all bytes");
|
||||||
|
stream.Close();
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,37 +65,47 @@ public static class 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteAllText(IOPath file, string content) =>
|
public static void WriteAllText(IOPath file, string content) => WriteAllBytes(file, content.ToBytes(StringConverter.UTF8));
|
||||||
WriteAllBytes(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);
|
||||||
|
stream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
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 System.IO.FileStream OpenRead(IOPath file)
|
public static System.IO.FileStream OpenRead(IOPath file)
|
||||||
{
|
{
|
||||||
if (!Exists(file))
|
if (!Exists(file))
|
||||||
throw new Exception($"file not found: <{file}>");
|
throw new Exception($"file not found: <{file}>");
|
||||||
return System.IO.File.Open(file.Str, FileMode.Open, FileAccess.Read,
|
return System.IO.File.Open(file.Str, System.IO.FileMode.Open, System.IO.FileAccess.Read,
|
||||||
System.IO.FileShare.ReadWrite | System.IO.FileShare.Delete);
|
System.IO.FileShare.ReadWrite | System.IO.FileShare.Delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static System.IO.FileStream OpenWrite(IOPath file)
|
public static System.IO.FileStream OpenWrite(IOPath file)
|
||||||
{
|
{
|
||||||
Directory.Create(file.ParentDir());
|
if (Exists(file))
|
||||||
return System.IO.File.Open(file.Str, FileMode.Create, FileAccess.Write, System.IO.FileShare.Read);
|
Delete(file);
|
||||||
|
Create(file);
|
||||||
|
return System.IO.File.Open(file.Str, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Read);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static System.IO.FileStream OpenAppend(IOPath file)
|
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);
|
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})");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -40,18 +40,18 @@ public readonly struct IOPath
|
|||||||
return new string(fixed_path);
|
return new string(fixed_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IOPath[] ArrayCast(string[] a, bool separatorsFixed=false)
|
public static IOPath[] ArrayCast(string[] a, bool correct_separators=false)
|
||||||
{
|
{
|
||||||
IOPath[] b = new IOPath[a.Length];
|
IOPath[] b = new IOPath[a.Length];
|
||||||
for (int i = 0; i < a.Length; i++)
|
for (int i = 0; i < a.Length; i++)
|
||||||
b[i] = new IOPath(a[i], separatorsFixed);
|
b[i] = new IOPath(a[i], correct_separators);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
public static IOPath[] ListCast(IList<string> a, bool separatorsFixed=false)
|
public static IOPath[] ListCast(IList<string> a, bool correct_separators=false)
|
||||||
{
|
{
|
||||||
IOPath[] b = new IOPath[a.Count];
|
IOPath[] b = new IOPath[a.Count];
|
||||||
for (int i = 0; i < a.Count; i++)
|
for (int i = 0; i < a.Count; i++)
|
||||||
b[i] = new IOPath(a[i], separatorsFixed);
|
b[i] = new IOPath(a[i], correct_separators);
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@ namespace DTLib.Filesystem;
|
|||||||
public static class Path
|
public static class Path
|
||||||
{
|
{
|
||||||
public static readonly char Sep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/';
|
public static readonly char Sep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/';
|
||||||
public static readonly char NotSep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '/' : '\\';
|
public static readonly char NotSep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '/' : '\\' ;
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void ThrowIfEscapes(this IOPath path)
|
public static void ThrowIfEscapes(this IOPath path)
|
||||||
@ -14,7 +14,7 @@ public static class Path
|
|||||||
if (path.Str.Contains(".."))
|
if (path.Str.Contains(".."))
|
||||||
throw new Exception($"path <{path}> uses <..>, that's not allowed");
|
throw new Exception($"path <{path}> uses <..>, that's not allowed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replaces characters restricted in filesystem path
|
/// Replaces characters restricted in filesystem path
|
||||||
public static IOPath ReplaceRestrictedChars(string str)
|
public static IOPath ReplaceRestrictedChars(string str)
|
||||||
{
|
{
|
||||||
@ -25,21 +25,16 @@ public static class Path
|
|||||||
char c = r[i];
|
char c = r[i];
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '\n':
|
case '\n': case '\r':
|
||||||
case '\r':
|
case ':': case ';':
|
||||||
case ':':
|
|
||||||
case ';':
|
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/': case '\\':
|
||||||
case '\\':
|
|
||||||
b.Append('-');
|
b.Append('-');
|
||||||
break;
|
break;
|
||||||
case '<':
|
case '<': case '>':
|
||||||
case '>':
|
case '?': case '|':
|
||||||
case '?':
|
|
||||||
case '|':
|
|
||||||
b.Append('_');
|
b.Append('_');
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
b.Append('\'');
|
b.Append('\'');
|
||||||
break;
|
break;
|
||||||
@ -51,23 +46,22 @@ public static class Path
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IOPath(b.ToString(), true);
|
return new IOPath(b.ToString(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !USE_SPAN
|
#if !USE_SPAN
|
||||||
private static void CopyTo(this string s, char[] b, int startIndex)
|
private static void CopyTo(this string s, char[] b, int startIndex)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < s.Length; i++)
|
for (int i = 0; i < s.Length; i++)
|
||||||
b[startIndex + i] = s[i];
|
b[startIndex+i] = s[i];
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public static IOPath Concat(params IOPath[] parts)
|
public static IOPath Concat(params IOPath[] parts)
|
||||||
{
|
{
|
||||||
var needSeparator = new bool[parts.Length - 1];
|
var needSeparator = new bool[parts.Length-1];
|
||||||
int lengthSum = 0;
|
int lengthSum = 0;
|
||||||
for (int i = 0; i < parts.Length - 1; i++)
|
for (int i = 0; i < parts.Length-1; i++)
|
||||||
{
|
{
|
||||||
lengthSum += parts[i].Length;
|
lengthSum += parts[i].Length;
|
||||||
if (!parts[i].Str.EndsWith(Sep) && !parts[i + 1].Str.StartsWith(Sep))
|
if (!parts[i].Str.EndsWith(Sep) && !parts[i + 1].Str.StartsWith(Sep))
|
||||||
@ -77,19 +71,18 @@ public static class Path
|
|||||||
}
|
}
|
||||||
else needSeparator[i] = false;
|
else needSeparator[i] = false;
|
||||||
}
|
}
|
||||||
|
lengthSum += parts[parts.Length-1].Length;
|
||||||
lengthSum += parts[parts.Length - 1].Length;
|
|
||||||
var buffer = new char[lengthSum];
|
var buffer = new char[lengthSum];
|
||||||
parts[0].Str.CopyTo(buffer, 0);
|
parts[0].Str.CopyTo(buffer, 0);
|
||||||
int copiedChars = parts[0].Length;
|
int copiedChars = parts[0].Length;
|
||||||
for (int i = 1; i < parts.Length; i++)
|
for (int i = 1; i < parts.Length; i++)
|
||||||
{
|
{
|
||||||
if (needSeparator[i - 1])
|
if (needSeparator[i-1])
|
||||||
buffer[copiedChars++] = Sep;
|
buffer[copiedChars++] = Sep;
|
||||||
parts[i].Str.CopyTo(buffer, copiedChars);
|
parts[i].Str.CopyTo(buffer, copiedChars);
|
||||||
copiedChars += parts[i].Length;
|
copiedChars += parts[i].Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new IOPath(new string(buffer), true);
|
return new IOPath(new string(buffer), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,15 +91,15 @@ public static class Path
|
|||||||
{
|
{
|
||||||
int i = path.LastIndexOf(Sep);
|
int i = path.LastIndexOf(Sep);
|
||||||
if (i == path.Length - 1) // ends with separator
|
if (i == path.Length - 1) // ends with separator
|
||||||
i = path.LastIndexOf(Sep, i - 1);
|
i = path.LastIndexOf(Sep, i-1);
|
||||||
if (i == -1) return path;
|
if (i == -1) return path;
|
||||||
return path.Substring(i + 1);
|
return path.Substring(i+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IOPath Extension(this IOPath path)
|
public static IOPath Extension(this IOPath path)
|
||||||
{
|
{
|
||||||
int i = path.LastIndexOf('.');
|
int i = path.LastIndexOf('.');
|
||||||
if (i == -1)
|
if (i == -1)
|
||||||
return LastName(path);
|
return LastName(path);
|
||||||
return path.Substring(i + 1);
|
return path.Substring(i + 1);
|
||||||
}
|
}
|
||||||
@ -114,7 +107,7 @@ public static class Path
|
|||||||
public static IOPath RemoveExtension(this IOPath path)
|
public static IOPath RemoveExtension(this IOPath path)
|
||||||
{
|
{
|
||||||
int i = path.LastIndexOf('.');
|
int i = path.LastIndexOf('.');
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
return path.Substring(0, i);
|
return path.Substring(0, i);
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
@ -123,10 +116,10 @@ public static class Path
|
|||||||
{
|
{
|
||||||
int i = path.LastIndexOf(Sep);
|
int i = path.LastIndexOf(Sep);
|
||||||
if (i == path.Length - 1) // ends with separator
|
if (i == path.Length - 1) // ends with separator
|
||||||
i = path.LastIndexOf(Sep, i - 1);
|
i = path.LastIndexOf(Sep, i-1);
|
||||||
if (i == -1) // no parent dir
|
if (i == -1) // no parent dir
|
||||||
return $".{Sep}";
|
return $".{Sep}";
|
||||||
return path.Remove(i + 1);
|
return path.Remove(i+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IOPath ReplaceBase(this IOPath path, IOPath baseDir, IOPath otherDir)
|
public static IOPath ReplaceBase(this IOPath path, IOPath baseDir, IOPath otherDir)
|
||||||
@ -140,6 +133,6 @@ public static class Path
|
|||||||
{
|
{
|
||||||
if (!path.StartsWith(baseDir))
|
if (!path.StartsWith(baseDir))
|
||||||
throw new Exception($"path <{path}> doesnt starts with <{baseDir}");
|
throw new Exception($"path <{path}> doesnt starts with <{baseDir}");
|
||||||
return path.Substring(baseDir.Length + 1);
|
return path.Substring(baseDir.Length+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
15
DTLib/Filesystem/Symlink.cs
Normal file
15
DTLib/Filesystem/Symlink.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
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