changed framework to dotnet6

This commit is contained in:
2021-12-31 00:18:05 +03:00
parent d04855bff2
commit a4d5df3fd6
26 changed files with 1194 additions and 1324 deletions

View File

@@ -1,159 +1,139 @@
using DTLib.Extensions;
using System;
using System.Collections.Generic;
namespace DTLib.Filesystem;
namespace DTLib.Filesystem
public static class Directory
{
public static class Directory
public static bool Exists(string dir) => System.IO.Directory.Exists(dir);
// создает папку, если её не существует
public static void Create(string dir)
{
public static bool Exists(string dir) => System.IO.Directory.Exists(dir);
// создает папку, если её не существует
public static void Create(string dir)
if (!Directory.Exists(dir))
{
if (!Directory.Exists(dir))
{
// проверяет существование папки, в которой нужно создать dir
if (dir.Contains("\\") && !Directory.Exists(dir.Remove(dir.LastIndexOf('\\'))))
Create(dir.Remove(dir.LastIndexOf('\\')));
System.IO.Directory.CreateDirectory(dir);
}
}
// копирует все файлы и папки
public static void Copy(string source_dir, string new_dir, bool owerwrite = false)
{
Create(new_dir);
var subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
for (int i = 0; i < subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for (int i = 0; i < files.Count; i++)
{
string f = files[i].Replace(source_dir, new_dir);
File.Copy(files[i], f, owerwrite);
//PublicLog.Log(new string[] {"g", $"file <", "c", files[i], "b", "> have copied to <", "c", newfile, "b", ">\n'" });
}
}
// копирует все файлы и папки и выдаёт список конфликтующих файлов
public static void Copy(string source_dir, string new_dir, out List<string> conflicts, bool owerwrite = false)
{
conflicts = new List<string>();
var subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
Create(new_dir);
for (int i = 0; i < subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for (int i = 0; i < files.Count; i++)
{
string newfile = files[i].Replace(source_dir, new_dir);
if (File.Exists(newfile))
conflicts.Add(newfile);
File.Copy(files[i], newfile, owerwrite);
//PublicLog.Log(new string[] {"g", $"file <", "c", files[i], "b", "> have copied to <", "c", newfile, "b", ">\n'" });
}
}
// удаляет папку со всеми подпапками и файлами
public static void Delete(string dir)
{
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--)
{
PublicLog.Log($"deleting {subdirs[i]}\n");
if (Directory.Exists(subdirs[i]))
System.IO.Directory.Delete(subdirs[i], true);
}
PublicLog.Log($"deleting {dir}\n");
if (Directory.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, string searchPattern) => System.IO.Directory.GetFiles(dir, searchPattern);
public static string[] GetDirectories(string dir) => System.IO.Directory.GetDirectories(dir);
// выдает список всех файлов
public static List<string> GetAllFiles(string dir)
{
var all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for (int i = 0; i < cur_files.Length; i++)
{
all_files.Add(cur_files[i]);
//PublicLog.Log(new string[] { "b", "file found: <", "c", cur_files[i], "b", ">\n" });
}
string[] cur_subdirs = Directory.GetDirectories(dir);
for (int i = 0; i < cur_subdirs.Length; i++)
{
//PublicLog.Log(new string[] { "b", "subdir found: <", "c", cur_subdirs[i], "b", ">\n" });
all_files.AddRange(GetAllFiles(cur_subdirs[i]));
}
return all_files;
}
// выдает список всех файлов и подпапок в папке
public static List<string> GetAllFiles(string dir, ref List<string> all_subdirs)
{
var all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for (int i = 0; i < cur_files.Length; i++)
{
all_files.Add(cur_files[i]);
//PublicLog.Log(new string[] { "b", "file found: <", "c", cur_files[i], "b", ">\n" });
}
string[] cur_subdirs = Directory.GetDirectories(dir);
for (int i = 0; i < cur_subdirs.Length; i++)
{
all_subdirs.Add(cur_subdirs[i]);
//PublicLog.Log(new string[] { "b", "subdir found: <", "c", cur_subdirs[i], "b", ">\n" });
all_files.AddRange(GetAllFiles(cur_subdirs[i], ref all_subdirs));
}
return all_files;
}
public static string GetCurrent() => System.IO.Directory.GetCurrentDirectory();
public static void GrantAccess(string fullPath)
{
var dirInfo = new System.IO.DirectoryInfo(fullPath);
System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();
dirSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(
new System.Security.Principal.SecurityIdentifier(
System.Security.Principal.WellKnownSidType.WorldSid, null),
System.Security.AccessControl.FileSystemRights.FullControl,
System.Security.AccessControl.InheritanceFlags.ObjectInherit |
System.Security.AccessControl.InheritanceFlags.ContainerInherit,
System.Security.AccessControl.PropagationFlags.NoPropagateInherit,
System.Security.AccessControl.AccessControlType.Allow));
dirInfo.SetAccessControl(dirSecurity);
}
public static void CreateSymlink(string sourceName, string symlinkName)
{
if (symlinkName.Contains("\\"))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\')));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
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)
{
var files = Directory.GetAllFiles(srcdir);
if (!srcdir.EndsWith('\\')) srcdir += '\\';
if (!newdir.EndsWith('\\')) newdir += '\\';
int i = 0;
for (; i < files.Count; i++)
File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir));
return i;
// проверяет существование папки, в которой нужно создать dir
if (dir.Contains("\\") && !Directory.Exists(dir.Remove(dir.LastIndexOf('\\'))))
Create(dir.Remove(dir.LastIndexOf('\\')));
System.IO.Directory.CreateDirectory(dir);
}
}
// копирует все файлы и папки
public static void Copy(string source_dir, string new_dir, bool owerwrite = false)
{
Create(new_dir);
var subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
for (int i = 0; i < subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for (int i = 0; i < files.Count; i++)
{
string f = files[i].Replace(source_dir, new_dir);
File.Copy(files[i], f, owerwrite);
//PublicLog.Log(new string[] {"g", $"file <", "c", files[i], "b", "> have copied to <", "c", newfile, "b", ">\n'" });
}
}
// копирует все файлы и папки и выдаёт список конфликтующих файлов
public static void Copy(string source_dir, string new_dir, out List<string> conflicts, bool owerwrite = false)
{
conflicts = new List<string>();
var subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
Create(new_dir);
for (int i = 0; i < subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for (int i = 0; i < files.Count; i++)
{
string newfile = files[i].Replace(source_dir, new_dir);
if (File.Exists(newfile))
conflicts.Add(newfile);
File.Copy(files[i], newfile, owerwrite);
//PublicLog.Log(new string[] {"g", $"file <", "c", files[i], "b", "> have copied to <", "c", newfile, "b", ">\n'" });
}
}
// удаляет папку со всеми подпапками и файлами
public static void Delete(string dir)
{
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--)
{
PublicLog.Log($"deleting {subdirs[i]}\n");
if (Directory.Exists(subdirs[i]))
System.IO.Directory.Delete(subdirs[i], true);
}
PublicLog.Log($"deleting {dir}\n");
if (Directory.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, string searchPattern) => System.IO.Directory.GetFiles(dir, searchPattern);
public static string[] GetDirectories(string dir) => System.IO.Directory.GetDirectories(dir);
// выдает список всех файлов
public static List<string> GetAllFiles(string dir)
{
var all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for (int i = 0; i < cur_files.Length; i++)
{
all_files.Add(cur_files[i]);
//PublicLog.Log(new string[] { "b", "file found: <", "c", cur_files[i], "b", ">\n" });
}
string[] cur_subdirs = Directory.GetDirectories(dir);
for (int i = 0; i < cur_subdirs.Length; i++)
{
//PublicLog.Log(new string[] { "b", "subdir found: <", "c", cur_subdirs[i], "b", ">\n" });
all_files.AddRange(GetAllFiles(cur_subdirs[i]));
}
return all_files;
}
// выдает список всех файлов и подпапок в папке
public static List<string> GetAllFiles(string dir, ref List<string> all_subdirs)
{
var all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for (int i = 0; i < cur_files.Length; i++)
{
all_files.Add(cur_files[i]);
//PublicLog.Log(new string[] { "b", "file found: <", "c", cur_files[i], "b", ">\n" });
}
string[] cur_subdirs = Directory.GetDirectories(dir);
for (int i = 0; i < cur_subdirs.Length; i++)
{
all_subdirs.Add(cur_subdirs[i]);
//PublicLog.Log(new string[] { "b", "subdir found: <", "c", cur_subdirs[i], "b", ">\n" });
all_files.AddRange(GetAllFiles(cur_subdirs[i], ref all_subdirs));
}
return all_files;
}
public static string GetCurrent() => System.IO.Directory.GetCurrentDirectory();
public static void CreateSymlink(string sourceName, string symlinkName)
{
if (symlinkName.Contains("\\"))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\')));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
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)
{
var files = Directory.GetAllFiles(srcdir);
if (!srcdir.EndsWith('\\')) srcdir += '\\';
if (!newdir.EndsWith('\\')) newdir += '\\';
int i = 0;
for (; i < files.Count; i++)
File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir));
return i;
}
}

View File

@@ -1,87 +1,83 @@
using DTLib.Extensions;
using System;
namespace DTLib.Filesystem;
namespace DTLib.Filesystem
public static class File
{
public static class File
public static int GetSize(string file) => new System.IO.FileInfo(file).Length.ToInt();
public static bool Exists(string file) => System.IO.File.Exists(file);
// если файл не существует, создаёт файл, создаёт папки из его пути
public static void Create(string file, bool delete_old = false)
{
public static int GetSize(string file) => new System.IO.FileInfo(file).Length.ToInt();
public static bool Exists(string file) => System.IO.File.Exists(file);
// если файл не существует, создаёт файл, создаёт папки из его пути
public static void Create(string file, bool delete_old = false)
if (delete_old && File.Exists(file))
File.Delete(file);
if (!File.Exists(file))
{
if (delete_old && File.Exists(file))
File.Delete(file);
if (!File.Exists(file))
{
if (file.Contains("\\"))
Directory.Create(file.Remove(file.LastIndexOf('\\')));
using System.IO.FileStream stream = System.IO.File.Create(file);
stream.Close();
}
}
public static void Copy(string srcPath, string newPath, bool replace = false)
{
if (!replace && Exists(newPath))
throw new Exception($"file <{newPath}> alredy exists");
Create(newPath);
WriteAllBytes(newPath, ReadAllBytes(srcPath));
}
public static void Delete(string file) => System.IO.File.Delete(file);
public static byte[] ReadAllBytes(string file)
{
using System.IO.FileStream stream = File.OpenRead(file);
int size = GetSize(file);
byte[] output = new byte[size];
stream.Read(output, 0, size);
if (file.Contains("\\"))
Directory.Create(file.Remove(file.LastIndexOf('\\')));
using System.IO.FileStream stream = System.IO.File.Create(file);
stream.Close();
return output;
}
public static string ReadAllText(string file) => ReadAllBytes(file).BytesToString();
public static void WriteAllBytes(string file, byte[] content)
{
using System.IO.FileStream stream = File.OpenWrite(file);
stream.Write(content, 0, content.Length);
stream.Close();
}
public static void WriteAllText(string file, string content) => WriteAllBytes(file, content.ToBytes());
public static void AppendAllBytes(string file, byte[] content)
{
using System.IO.FileStream stream = File.OpenAppend(file);
stream.Write(content, 0, content.Length);
stream.Close();
}
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes());
public static System.IO.FileStream OpenRead(string file) =>
Exists(file) ? System.IO.File.OpenRead(file) : throw new Exception($"file not found: <{file}>");
public static System.IO.FileStream OpenWrite(string file)
{
File.Create(file, true);
return System.IO.File.Open(file, System.IO.FileMode.OpenOrCreate);
}
public static System.IO.FileStream OpenAppend(string file)
{
File.Create(file);
return System.IO.File.Open(file, System.IO.FileMode.Append);
}
public static void CreateSymlink(string sourceName, string symlinkName)
{
if (symlinkName.Contains("\\"))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\')));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})");
}
}
public static void Copy(string srcPath, string newPath, bool replace = false)
{
if (!replace && Exists(newPath))
throw new Exception($"file <{newPath}> alredy exists");
Create(newPath);
WriteAllBytes(newPath, ReadAllBytes(srcPath));
}
public static void Delete(string file) => System.IO.File.Delete(file);
public static byte[] ReadAllBytes(string file)
{
using System.IO.FileStream stream = File.OpenRead(file);
int size = GetSize(file);
byte[] output = new byte[size];
stream.Read(output, 0, size);
stream.Close();
return output;
}
public static string ReadAllText(string file) => ReadAllBytes(file).BytesToString();
public static void WriteAllBytes(string file, byte[] content)
{
using System.IO.FileStream stream = File.OpenWrite(file);
stream.Write(content, 0, content.Length);
stream.Close();
}
public static void WriteAllText(string file, string content) => WriteAllBytes(file, content.ToBytes());
public static void AppendAllBytes(string file, byte[] content)
{
using System.IO.FileStream stream = File.OpenAppend(file);
stream.Write(content, 0, content.Length);
stream.Close();
}
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes());
public static System.IO.FileStream OpenRead(string file) =>
Exists(file) ? System.IO.File.OpenRead(file) : throw new Exception($"file not found: <{file}>");
public static System.IO.FileStream OpenWrite(string file)
{
File.Create(file, true);
return System.IO.File.Open(file, System.IO.FileMode.OpenOrCreate);
}
public static System.IO.FileStream OpenAppend(string file)
{
File.Create(file);
return System.IO.File.Open(file, System.IO.FileMode.Append);
}
public static void CreateSymlink(string sourceName, string symlinkName)
{
if (symlinkName.Contains("\\"))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\')));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})");
}
}

View File

@@ -1,68 +1,65 @@
using System;
namespace DTLib.Filesystem;
namespace DTLib.Filesystem
//
// некоторые старые методы, которые хорошо бы вырезать
//
public static class OldFilework
{
//
// некоторые старые методы, которые хорошо бы вырезать
//
public static class OldFilework
// записывает текст в файл и закрывает файл
/*public static void LogToFile(string logfile, string msg)
{
// записывает текст в файл и закрывает файл
/*public static void LogToFile(string logfile, string msg)
lock (new object())
{
lock (new object())
{
File.AppendAllText(logfile, msg);
}
}*/
File.AppendAllText(logfile, msg);
}
}*/
// чтение параметров из конфига
public static string ReadFromConfig(string configfile, string key)
// чтение параметров из конфига
public static string ReadFromConfig(string configfile, string key)
{
lock (new object())
{
lock (new object())
key += ": ";
using var reader = new System.IO.StreamReader(configfile);
while (!reader.EndOfStream)
{
key += ": ";
using var reader = new System.IO.StreamReader(configfile);
while (!reader.EndOfStream)
string st = reader.ReadLine();
if (st.StartsWith(key))
{
string st = reader.ReadLine();
if (st.StartsWith(key))
string value = "";
for (int i = key.Length; i < st.Length; i++)
{
string value = "";
for (int i = key.Length; i < st.Length; i++)
if (st[i] == '#')
return value;
if (st[i] == '%')
{
if (st[i] == '#')
return value;
if (st[i] == '%')
bool stop = false;
string placeholder = "";
i++;
while (!stop)
{
bool stop = false;
string placeholder = "";
i++;
while (!stop)
if (st[i] == '%')
{
if (st[i] == '%')
{
stop = true;
value += ReadFromConfig(configfile, placeholder);
}
else
{
placeholder += st[i];
i++;
}
stop = true;
value += ReadFromConfig(configfile, placeholder);
}
else
{
placeholder += st[i];
i++;
}
}
else
value += st[i];
}
reader.Close();
//if (value == "") throw new System.Exception($"ReadFromConfig({configfile}, {key}) error: key not found");
return value;
else
value += st[i];
}
reader.Close();
//if (value == "") throw new System.Exception($"ReadFromConfig({configfile}, {key}) error: key not found");
return value;
}
reader.Close();
throw new Exception($"ReadFromConfig({configfile}, {key}) error: key not found");
}
reader.Close();
throw new Exception($"ReadFromConfig({configfile}, {key}) error: key not found");
}
}
}

View File

@@ -1,21 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DTLib.Filesystem
namespace DTLib.Filesystem;
internal class Symlink
{
internal class Symlink
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static internal extern bool CreateSymbolicLink(string symlinkName, string sourceName, SymlinkTarget type);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static internal extern bool CreateSymbolicLink(string symlinkName, string sourceName, SymlinkTarget type);
internal enum SymlinkTarget
{
File,
Directory
}
internal enum SymlinkTarget
{
File,
Directory
}
}