refactoring

This commit is contained in:
2021-10-26 19:05:31 +03:00
parent dcc1ad3e17
commit 5538178cab
18 changed files with 405 additions and 457 deletions

View File

@@ -10,10 +10,10 @@ namespace DTLib.Filesystem
// создает папку, если её не существует
public static void Create(string dir)
{
if(!Directory.Exists(dir))
if (!Directory.Exists(dir))
{
// проверяет существование папки, в которой нужно создать dir
if(dir.Contains("\\")&&!Directory.Exists(dir.Remove(dir.LastIndexOf('\\'))))
if (dir.Contains("\\") && !Directory.Exists(dir.Remove(dir.LastIndexOf('\\'))))
Create(dir.Remove(dir.LastIndexOf('\\')));
System.IO.Directory.CreateDirectory(dir);
}
@@ -22,13 +22,13 @@ namespace DTLib.Filesystem
public static void Copy(string source_dir, string new_dir, bool owerwrite = false)
{
Create(new_dir);
var subdirs = new List<string>();
List<string> subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
for(int i = 0; i<subdirs.Count; i++)
for (int i = 0; i < subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for(int i = 0; i<files.Count; i++)
for (int i = 0; i < files.Count; i++)
{
string f = files[i].Replace(source_dir, new_dir);
File.Copy(files[i], f, owerwrite);
@@ -39,18 +39,18 @@ namespace DTLib.Filesystem
// копирует все файлы и папки и выдаёт список конфликтующих файлов
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>();
conflicts = new List<string>();
List<string> subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
Create(new_dir);
for(int i = 0; i<subdirs.Count; i++)
for (int i = 0; i < subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for(int i = 0; i<files.Count; i++)
for (int i = 0; i < files.Count; i++)
{
string newfile = files[i].Replace(source_dir, new_dir);
if(File.Exists(newfile))
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'" });
@@ -60,18 +60,18 @@ namespace DTLib.Filesystem
// удаляет папку со всеми подпапками и файлами
public static void Delete(string dir)
{
var subdirs = new List<string>();
List<string> subdirs = new List<string>();
List<string> files = GetAllFiles(dir, ref subdirs);
for(int i = 0; i<files.Count; i++)
for (int i = 0; i < files.Count; i++)
File.Delete(files[i]);
for(int i = subdirs.Count-1; i>=0; i--)
for (int i = subdirs.Count - 1; i >= 0; i--)
{
PublicLog.Log($"deleting {subdirs[i]}\n");
if(Directory.Exists(subdirs[i]))
if (Directory.Exists(subdirs[i]))
System.IO.Directory.Delete(subdirs[i], true);
}
PublicLog.Log($"deleting {dir}\n");
if(Directory.Exists(dir))
if (Directory.Exists(dir))
System.IO.Directory.Delete(dir, true);
}
@@ -82,15 +82,15 @@ namespace DTLib.Filesystem
// выдает список всех файлов
public static List<string> GetAllFiles(string dir)
{
var all_files = new List<string>();
List<string> all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for(int i = 0; i<cur_files.Length; i++)
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++)
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]));
@@ -101,15 +101,15 @@ namespace DTLib.Filesystem
// выдает список всех файлов и подпапок в папке
public static List<string> GetAllFiles(string dir, ref List<string> all_subdirs)
{
var all_files = new List<string>();
List<string> all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for(int i = 0; i<cur_files.Length; i++)
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++)
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" });
@@ -122,13 +122,13 @@ namespace DTLib.Filesystem
public static void GrantAccess(string fullPath)
{
var dirInfo = new System.IO.DirectoryInfo(fullPath);
System.IO.DirectoryInfo 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.ObjectInherit |
System.Security.AccessControl.InheritanceFlags.ContainerInherit,
System.Security.AccessControl.PropagationFlags.NoPropagateInherit,
System.Security.AccessControl.AccessControlType.Allow));

View File

@@ -11,11 +11,11 @@ namespace DTLib.Filesystem
// если файл не существует, создаёт файл, создаёт папки из его пути
public static void Create(string file, bool delete_old = false)
{
if(delete_old&&File.Exists(file))
if (delete_old && File.Exists(file))
File.Delete(file);
if(!File.Exists(file))
if (!File.Exists(file))
{
if(file.Contains("\\"))
if (file.Contains("\\"))
Directory.Create(file.Remove(file.LastIndexOf('\\')));
using System.IO.FileStream stream = System.IO.File.Create(file);
stream.Close();
@@ -24,7 +24,7 @@ namespace DTLib.Filesystem
public static void Copy(string srcPath, string newPath, bool replace = false)
{
if(!replace&&Exists(newPath))
if (!replace && Exists(newPath))
throw new Exception($"file <{newPath}> alredy exists");
Create(newPath);
WriteAllBytes(newPath, ReadAllBytes(srcPath));
@@ -62,12 +62,8 @@ namespace DTLib.Filesystem
public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes());
public static System.IO.FileStream OpenRead(string file)
{
if(!Exists(file))
throw new Exception($"file not found: <{file}>");
return System.IO.File.OpenRead(file);
}
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);

View File

@@ -10,7 +10,7 @@ namespace DTLib.Filesystem
// записывает текст в файл и закрывает файл
public static void LogToFile(string logfile, string msg)
{
lock(new object())
lock (new object())
{
File.AppendAllText(logfile, msg);
}
@@ -19,41 +19,41 @@ namespace DTLib.Filesystem
// чтение параметров из конфига
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 System.IO.StreamReader reader = new System.IO.StreamReader(configfile);
while (!reader.EndOfStream)
{
string st = reader.ReadLine();
if(st.StartsWith(key))
if (st.StartsWith(key))
{
string value = "";
for(int i = key.Length; i<st.Length; i++)
for (int i = key.Length; i < st.Length; i++)
{
if(st[i]=='#')
if (st[i] == '#')
return value;
if(st[i]=='%')
if (st[i] == '%')
{
bool stop = false;
string placeholder = "";
i++;
while(!stop)
while (!stop)
{
if(st[i]=='%')
if (st[i] == '%')
{
stop=true;
value+=ReadFromConfig(configfile, placeholder);
stop = true;
value += ReadFromConfig(configfile, placeholder);
}
else
{
placeholder+=st[i];
placeholder += st[i];
i++;
}
}
}
else
value+=st[i];
value += st[i];
}
reader.Close();
//if (value == "") throw new System.Exception($"ReadFromConfig({configfile}, {key}) error: key not found");