148 lines
4.5 KiB
C#
148 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Mitria_Minecraft_Launcher
|
|
{
|
|
public static class Extensions
|
|
{
|
|
public static bool EmptyDirectory(string location)
|
|
{
|
|
bool error = false;
|
|
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(location);
|
|
|
|
foreach (var fi in directoryInfo.EnumerateFiles())
|
|
{
|
|
try
|
|
{
|
|
fi.IsReadOnly = false;
|
|
fi.Delete();
|
|
|
|
while (fi.Exists)
|
|
{
|
|
System.Threading.Thread.Sleep(10);
|
|
fi.Refresh();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
CommonLibrary.Log.ERROR(e.Message);
|
|
error = true;
|
|
}
|
|
}
|
|
foreach (var di in directoryInfo.EnumerateDirectories())
|
|
{
|
|
try
|
|
{
|
|
EmptyDirectory(di.FullName);
|
|
di.Delete();
|
|
while (di.Exists)
|
|
{
|
|
System.Threading.Thread.Sleep(10);
|
|
di.Refresh();
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
CommonLibrary.Log.ERROR(e.Message);
|
|
error = true;
|
|
}
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
static void DeleteFile(string filePath)
|
|
{
|
|
try
|
|
{
|
|
File.SetAttributes(filePath, FileAttributes.Normal); // 읽기 전용 제거
|
|
File.Delete(filePath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("파일 삭제 실패: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
static void DeleteDirectory(string dirPath)
|
|
{
|
|
try
|
|
{
|
|
// 하위 파일 속성 변경 및 삭제
|
|
foreach (string file in Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories))
|
|
{
|
|
File.SetAttributes(file, FileAttributes.Normal);
|
|
}
|
|
|
|
// 디렉터리 삭제 (재귀적)
|
|
Directory.Delete(dirPath, true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("디렉터리 삭제 실패: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static string ApplyExcuteCommand(ExcuteArgument excuteArgument)
|
|
{
|
|
string command = excuteArgument.Argument;
|
|
string regexPattern = @"\{[A-z]*\}";
|
|
Regex regex = new Regex(regexPattern);
|
|
MatchCollection resultCollection = regex.Matches(command);
|
|
List<string> commandList = new List<string>();
|
|
foreach (Match match in resultCollection)
|
|
{
|
|
string key = match.Value.Substring(1, match.Value.Length - 2);
|
|
if (!commandList.Contains(key))
|
|
{
|
|
commandList.Add(key);
|
|
}
|
|
}
|
|
|
|
foreach (string commandKey in commandList)
|
|
{
|
|
Parameter option = excuteArgument.Parameters.Find(x => x.Key == commandKey);
|
|
command = command.Replace("{" + commandKey + "}", option.Value);
|
|
}
|
|
|
|
return command;
|
|
}
|
|
|
|
public static bool IsDefault<T>(ref this T data) where T : struct
|
|
{
|
|
return default(T).Equals(data);
|
|
}
|
|
|
|
public static string UnitSeparator(this long value)
|
|
{
|
|
return string.Format("{0:#,0}", (object)value);
|
|
}
|
|
|
|
public static string UnitSeparator(this int value)
|
|
{
|
|
return string.Format("{0:#,0}", (object)value);
|
|
}
|
|
|
|
public static string GetFileHashCode(this FileInfo file)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
{
|
|
using (Stream stream = file.OpenRead())
|
|
{
|
|
byte[] hashCode = md5.ComputeHash(stream);
|
|
StringBuilder hashStringBuilder = new StringBuilder();
|
|
foreach (byte b in hashCode)
|
|
{
|
|
hashStringBuilder.AppendFormat("{0:x2}", b);
|
|
}
|
|
return hashStringBuilder.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |