144 lines
3.1 KiB
C#
144 lines
3.1 KiB
C#
using System.Text;
|
|
|
|
namespace RdtClient.Service.Helpers;
|
|
|
|
public static class FileHelper
|
|
{
|
|
public static async Task Delete(String path)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(path))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!File.Exists(path))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var retry = 0;
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(path);
|
|
|
|
break;
|
|
}
|
|
catch
|
|
{
|
|
if (retry >= 3)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
retry++;
|
|
|
|
await Task.Delay(1000 * retry);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static async Task DeleteDirectory(String path)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(path))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!Directory.Exists(path))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var retry = 0;
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
Directory.Delete(path, true);
|
|
|
|
break;
|
|
}
|
|
catch
|
|
{
|
|
if (retry >= 3)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
retry++;
|
|
|
|
await Task.Delay(1000 * retry);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static String RemoveInvalidFileNameChars(String filename)
|
|
{
|
|
var invalidChars = Path.GetInvalidFileNameChars()
|
|
.Concat(['/', '\\'])
|
|
.Distinct()
|
|
.ToArray();
|
|
|
|
var cleaned = String.Concat(filename.Split(invalidChars));
|
|
cleaned = cleaned.Replace("..", "");
|
|
cleaned = cleaned.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
|
|
|
return cleaned;
|
|
}
|
|
|
|
public static String GetDirectoryContents(String path)
|
|
{
|
|
var stringBuilder = new StringBuilder();
|
|
GetDirectoryContents(path, stringBuilder, "");
|
|
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
private static void GetDirectoryContents(String path, StringBuilder stringBuilder, String indent)
|
|
{
|
|
var directoryInfo = new DirectoryInfo(path);
|
|
|
|
var directories = directoryInfo.GetDirectories();
|
|
|
|
foreach (var directory in directories)
|
|
{
|
|
stringBuilder.AppendLine($"{indent}{directory.Name}");
|
|
GetDirectoryContents(directory.FullName, stringBuilder, indent + " ");
|
|
}
|
|
|
|
var files = directoryInfo.GetFiles();
|
|
|
|
foreach (var file in files)
|
|
{
|
|
stringBuilder.AppendLine($"{indent}{file.Name}");
|
|
}
|
|
}
|
|
|
|
public static Int64 GetAvailableFreeSpaceGB(String path)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(path))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
try
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var driveInfo = new DriveInfo(path);
|
|
|
|
return driveInfo.AvailableFreeSpace / (1024 * 1024 * 1024);
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|