diff --git a/Netch.sln b/Netch.sln index e74d69de..12a0de1a 100644 --- a/Netch.sln +++ b/Netch.sln @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Netch", "Netch\Netch.csproj EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetchLib", "NetchLib\NetchLib.csproj", "{A8715AF4-ACC6-43F9-9381-4294C5360623}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetchUpdater", "NetchUpdater\NetchUpdater.csproj", "{828318A8-9B90-4A5F-BD6B-E632CC9D8933}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{53397641-35CA-4336-8E22-2CE12EF476AC}" EndProject Global @@ -25,10 +23,6 @@ Global {A8715AF4-ACC6-43F9-9381-4294C5360623}.Debug|x64.Build.0 = Debug|x64 {A8715AF4-ACC6-43F9-9381-4294C5360623}.Release|x64.ActiveCfg = Release|x64 {A8715AF4-ACC6-43F9-9381-4294C5360623}.Release|x64.Build.0 = Release|x64 - {828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Debug|x64.ActiveCfg = Debug|x64 - {828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Debug|x64.Build.0 = Debug|x64 - {828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Release|x64.ActiveCfg = Release|x64 - {828318A8-9B90-4A5F-BD6B-E632CC9D8933}.Release|x64.Build.0 = Release|x64 {53397641-35CA-4336-8E22-2CE12EF476AC}.Debug|x64.ActiveCfg = Debug|x64 {53397641-35CA-4336-8E22-2CE12EF476AC}.Debug|x64.Build.0 = Debug|x64 {53397641-35CA-4336-8E22-2CE12EF476AC}.Release|x64.ActiveCfg = Release|x64 diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs index df5cfa2c..df80d2ba 100644 --- a/Netch/Controllers/UpdateChecker.cs +++ b/Netch/Controllers/UpdateChecker.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Net; using System.Text.RegularExpressions; @@ -8,6 +7,7 @@ using System.Threading.Tasks; using Netch.Models.GitHubRelease; using Netch.Utils; using Newtonsoft.Json; +using static Netch.Updater.Updater; namespace Netch.Controllers { @@ -19,7 +19,7 @@ namespace Netch.Controllers public const string Name = @"Netch"; public const string Copyright = @"Copyright © 2019 - 2021"; - public const string AssemblyVersion = @"1.7.4"; + public const string AssemblyVersion = @"1.7.3"; private const string Suffix = @""; public static readonly string Version = $"{AssemblyVersion}{(string.IsNullOrEmpty(Suffix) ? "" : $"-{Suffix}")}"; @@ -68,7 +68,7 @@ namespace Netch.Controllers } } - public static async Task UpdateNetch(DownloadProgressChangedEventHandler onDownloadProgressChanged) + public static async Task DownloadUpdate(DownloadProgressChangedEventHandler onDownloadProgressChanged) { using WebClient client = new(); @@ -87,7 +87,7 @@ namespace Netch.Controllers { if (Utils.Utils.SHA256CheckSum(fileFullPath) == sha256) { - RunUpdater(); + UpdateNetch(fileFullPath); return; } @@ -108,21 +108,7 @@ namespace Netch.Controllers if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256) throw new Exception(i18N.Translate("The downloaded file has the wrong hash")); - RunUpdater(); - - void RunUpdater() - { - // if debugging process stopped, debugger will kill child processes!!!! - // 调试进程结束,调试器将会杀死子进程 - // uncomment if(!Debugger.isAttach) block in NetchUpdater Project's main() method and attach to NetchUpdater process to debug - // 在 NetchUpdater 项目的 main() 方法中取消注释 if(!Debugger.isAttach)块,并附加到 NetchUpdater 进程进行调试 - Process.Start(new ProcessStartInfo - { - FileName = Path.Combine(Global.NetchDir, "NetchUpdater.exe"), - Arguments = - $"{Global.Settings.UDPSocketPort} \"{fileFullPath}\" \"{Global.NetchDir}\"" - }); - } + UpdateNetch(fileFullPath); } } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 7a100595..908f85ad 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1346,7 +1346,7 @@ namespace Netch.Forms Hide(); } - private async void Exit(bool forceExit = false) + public async void Exit(bool forceExit = false) { if (!IsWaiting() && !Global.Settings.StopWhenExited && !forceExit) { @@ -1441,7 +1441,7 @@ namespace Netch.Forms BeginInvoke(new Action(() => { NewVersionLabel.Text = $"{args.ProgressPercentage}%"; })); } - await UpdateChecker.UpdateNetch(OnDownloadProgressChanged); + await UpdateChecker.DownloadUpdate(OnDownloadProgressChanged); } catch (Exception exception) { diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index e50d4289..48d5708a 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; @@ -69,43 +68,11 @@ namespace Netch.Forms.Mode { try { - var RDirInfo = new DirectoryInfo(DirName); - if (!RDirInfo.Exists) - { - return; - } + RuleListBox.Items.AddRange(Directory.GetFiles(DirName, "*.exe", SearchOption.AllDirectories)); } - catch (Exception) + catch (Exception e) { - return; - } - - var DirStack = new Stack(); - DirStack.Push(DirName); - - while (DirStack.Count > 0) - { - var DirInfo = new DirectoryInfo(DirStack.Pop()); - try - { - foreach (var DirChildInfo in DirInfo.GetDirectories()) - { - DirStack.Push(DirChildInfo.FullName); - } - - foreach (var FileChildInfo in DirInfo.GetFiles()) - { - if (FileChildInfo.Name.EndsWith(".exe") && !RuleListBox.Items.Contains(FileChildInfo.Name)) - { - RuleListBox.Items.Add(FileChildInfo.Name); - Edited = true; - } - } - } - catch (Exception) - { - // ignored - } + // ignored } } @@ -216,7 +183,8 @@ namespace Netch.Forms.Mode } else { - var fullName = ModeHelper.GetFullPath(FilenameTextBox.Text + ".txt"); + var relativePath = $"Custom\\{FilenameTextBox.Text}.txt"; + var fullName = ModeHelper.GetFullPath(relativePath); if (File.Exists(fullName)) { MessageBoxX.Show(i18N.Translate("File already exists.\n Please Change the filename")); @@ -227,6 +195,7 @@ namespace Netch.Forms.Mode { BypassChina = false, FileName = FilenameTextBox.Text, + RelativePath = relativePath, Type = 0, Remark = RemarkTextBox.Text }; diff --git a/Netch/Global.cs b/Netch/Global.cs index 7187b669..eb1cfceb 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; +using System.Threading; using System.Windows.Forms; using WindowsJobAPI; @@ -26,6 +27,8 @@ namespace Netch /// public static MainForm MainForm; + public static readonly Mutex Mutex = new(false, "Global\\Netch"); + public static class Flags { public static bool SupportFakeDns => _supportFakeDns ??= new TUNTAPController().TestFakeDNS(); diff --git a/Netch/Netch.cs b/Netch/Netch.cs index a034b957..50abb4a0 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -22,69 +22,67 @@ namespace Netch if (!NativeMethods.AttachConsole(-1)) NativeMethods.AllocConsole(); - // 创建互斥体防止多次运行 - using (var mutex = new Mutex(false, "Global\\Netch")) + // 设置当前目录 + Directory.SetCurrentDirectory(Global.NetchDir); + Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) + ";" + Path.Combine(Global.NetchDir, "bin"), EnvironmentVariableTarget.Process); + + Updater.Updater.CleanOld(); + + // 预创建目录 + var directories = new[] {"mode\\Custom", "data", "i18n", "logging"}; + foreach (var item in directories) + if (!Directory.Exists(item)) + Directory.CreateDirectory(item); + + // 加载配置 + Configuration.Load(); + + // 加载语言 + i18N.Load(Global.Settings.Language); + + if (!Directory.Exists("bin") || !Directory.EnumerateFileSystemEntries("bin").Any()) { - // 设置当前目录 - Directory.SetCurrentDirectory(Global.NetchDir); - Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) + ";" + Path.Combine(Global.NetchDir, "bin"), EnvironmentVariableTarget.Process); - - // 预创建目录 - var directories = new[] {"mode", "data", "i18n", "logging"}; - foreach (var item in directories) - if (!Directory.Exists(item)) - Directory.CreateDirectory(item); - - // 加载配置 - Configuration.Load(); - - // 加载语言 - i18N.Load(Global.Settings.Language); - - if (!Directory.Exists("bin") || !Directory.EnumerateFileSystemEntries("bin").Any()) - { - MessageBoxX.Show(i18N.Translate("Please extract all files then run the program!")); - Environment.Exit(2); - } - - // 检查是否已经运行 - if (!mutex.WaitOne(0, false)) - { - OnlyInstance.Send(OnlyInstance.Commands.Show); - Logging.Info("唤起单实例"); - - // 退出进程 - Environment.Exit(1); - } - - // 清理上一次的日志文件,防止淤积占用磁盘空间 - if (Directory.Exists("logging")) - { - var directory = new DirectoryInfo("logging"); - - foreach (var file in directory.GetFiles()) - file.Delete(); - - foreach (var dir in directory.GetDirectories()) - dir.Delete(true); - } - - Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}"); - Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Application.ExecutablePath)}"); }); - Task.Run(() => - { - Logging.Info("启动单实例"); - OnlyInstance.Server(); - }); - - // 绑定错误捕获 - Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); - Application.ThreadException += Application_OnException; - - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(Global.MainForm = new MainForm()); + MessageBoxX.Show(i18N.Translate("Please extract all files then run the program!")); + Environment.Exit(2); } + + // 检查是否已经运行 + if (!Global.Mutex.WaitOne(0, false)) + { + OnlyInstance.Send(OnlyInstance.Commands.Show); + Logging.Info("唤起单实例"); + + // 退出进程 + Environment.Exit(1); + } + + // 清理上一次的日志文件,防止淤积占用磁盘空间 + if (Directory.Exists("logging")) + { + var directory = new DirectoryInfo("logging"); + + foreach (var file in directory.GetFiles()) + file.Delete(); + + foreach (var dir in directory.GetDirectories()) + dir.Delete(true); + } + + Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}"); + Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Application.ExecutablePath)}"); }); + Task.Run(() => + { + Logging.Info("启动单实例"); + OnlyInstance.Server(); + }); + + // 绑定错误捕获 + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + Application.ThreadException += Application_OnException; + + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(Global.MainForm = new MainForm()); } public static void Application_OnException(object sender, ThreadExceptionEventArgs e) diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index 7ac149e9..17eb5814 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -65,7 +65,6 @@ - diff --git a/Netch/Properties/Resources.Designer.cs b/Netch/Properties/Resources.Designer.cs index 432c445e..92b3ba51 100644 --- a/Netch/Properties/Resources.Designer.cs +++ b/Netch/Properties/Resources.Designer.cs @@ -149,5 +149,15 @@ namespace Netch.Properties { return ((byte[])(obj)); } } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] _7za { + get { + object obj = ResourceManager.GetObject("7za", resourceCulture); + return ((byte[])(obj)); + } + } } } diff --git a/Netch/Properties/Resources.resx b/Netch/Properties/Resources.resx index ed8ebd4e..7978fd5b 100644 --- a/Netch/Properties/Resources.resx +++ b/Netch/Properties/Resources.resx @@ -169,4 +169,9 @@ PublicKeyToken=b03f5f7f11d50a3a + + ..\Resources\7za.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Netch/Updater/Updater.cs b/Netch/Updater/Updater.cs new file mode 100644 index 00000000..48a2ba71 --- /dev/null +++ b/Netch/Updater/Updater.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Security.AccessControl; +using System.Text; +using System.Windows.Forms; +using Netch.Controllers; +using Netch.Forms; +using Netch.Properties; +using Netch.Utils; + +namespace Netch.Updater +{ + public static class Updater + { + public static void UpdateNetch(string updateFilePath) + { + var tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + var extractPath = Path.Combine(tempFolder, "extract"); + int exitCode; + if ((exitCode = Extract(updateFilePath, extractPath, true, tempFolder)) != 0) + { + MessageBoxX.Show($"7za exit with code {exitCode}"); + return; + } + + foreach (var file in Directory.GetFiles(Global.NetchDir, "*", SearchOption.AllDirectories)) + { + if (new[] {"data", "mode\\Custom"}.ToList().Any(p => file.StartsWith(Path.Combine(Global.NetchDir, p)))) + continue; + + try + { + File.Move(file, file + ".old"); + } + catch + { + throw new Exception("Updater wasn't able to rename file: " + file); + } + } + + MoveDirectory(Path.Combine(extractPath, "Netch"), Global.NetchDir, true); + + Global.Mutex.ReleaseMutex(); + Process.Start(Application.ExecutablePath); + Global.MainForm.Exit(true); + } + + private static int Extract(string archiveFileName, string destDirName, bool overwrite, string tempFolder = null) + { + tempFolder ??= Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + var temp7za = Path.Combine(tempFolder, "7za.exe"); + archiveFileName = Path.GetFullPath(archiveFileName); + destDirName = Path.GetFullPath(destDirName); + + if (!Directory.Exists(tempFolder)) + Directory.CreateDirectory(tempFolder); + + if (!File.Exists(temp7za)) + File.WriteAllBytes(temp7za, Resources._7za); + + var argument = new StringBuilder(); + argument.Append($" x \"{archiveFileName}\" -o\"{destDirName}\" "); + if (overwrite) + argument.Append(" -y "); + + var process = Process.Start(new ProcessStartInfo + { + UseShellExecute = false, + WindowStyle = ProcessWindowStyle.Hidden, + FileName = temp7za, + Arguments = argument.ToString() + }); + process?.WaitForExit(); + return process?.ExitCode ?? 2; + } + + public static FileInfo FindFile(string filename, string directory) + { + var DirStack = new Stack(); + DirStack.Push(directory); + + while (DirStack.Count > 0) + { + var DirInfo = new DirectoryInfo(DirStack.Pop()); + try + { + foreach (var DirChildInfo in DirInfo.GetDirectories()) + { + DirStack.Push(DirChildInfo.FullName); + } + } + catch + { + // ignored + } + + try + { + foreach (var FileChildInfo in DirInfo.GetFiles()) + { + if (FileChildInfo.Name == filename) + { + return FileChildInfo; + } + } + } + catch + { + // ignored + } + } + + return null; + } + + private static void MoveDirectory(string sourceDirName, string destDirName, bool overwrite) + { + sourceDirName = Path.GetFullPath(sourceDirName); + destDirName = Path.GetFullPath(destDirName); + if (!overwrite) + { + Directory.Move(sourceDirName, destDirName); + } + else + { + foreach (var dir in Directory.GetDirectories(sourceDirName, "*", SearchOption.AllDirectories)) + { + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + } + foreach (var f in Directory.GetFiles(sourceDirName, "*", SearchOption.AllDirectories)) + { + try + { + File.Move(f, f.Replace(sourceDirName, destDirName)); + } + catch (Exception e) + { + throw new Exception("Updater wasn't able to move file: " + f); + } + } + } + } + + private static bool TestFileFree(string FileName) + { + try + { + File.Move(FileName, FileName); + return true; + } + catch + { + return false; + } + } + + public static void CleanOld() + { + foreach (var f in Directory.GetFiles(Global.NetchDir, "*.old", SearchOption.AllDirectories)) + File.Delete(f); + } + } +} \ No newline at end of file diff --git a/NetchUpdater/.gitignore b/NetchUpdater/.gitignore deleted file mode 100644 index 0a079b63..00000000 --- a/NetchUpdater/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/bin -/obj \ No newline at end of file diff --git a/NetchUpdater/NetchUpdater.csproj b/NetchUpdater/NetchUpdater.csproj deleted file mode 100644 index f854a2ee..00000000 --- a/NetchUpdater/NetchUpdater.csproj +++ /dev/null @@ -1,59 +0,0 @@ - - - - Exe - net48 - false - NetchUpdater.Program - x64 - false - latest - https://github.com/NetchX/Netch - - - - bin\x64\Debug\ - DEBUG;TRACE - true - false - - - - - - true - - false - - bin\x64\Release\ - - - - - - - - - - - - - - - - - - ResXFileCodeGenerator - Resources.Designer.cs - - - - - - True - True - Resources.resx - - - - diff --git a/NetchUpdater/Program.cs b/NetchUpdater/Program.cs deleted file mode 100644 index de46f201..00000000 --- a/NetchUpdater/Program.cs +++ /dev/null @@ -1,351 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net.Sockets; -using System.Text; -using System.Threading; -using NetchUpdater.Properties; - -namespace NetchUpdater -{ - internal class Program - { - private static readonly string UpdaterFullName; - private static readonly string UpdaterDirectory; - private static readonly string UpdaterFriendlyName; - private static readonly Process CurrentProcess; - - static Program() - { - CurrentProcess = Process.GetCurrentProcess(); - UpdaterFullName = CurrentProcess.MainModule.FileName; - UpdaterDirectory = Path.GetDirectoryName(UpdaterFullName); - UpdaterFriendlyName = Path.GetFileName(UpdaterFullName); - } - - public static void Main(string[] args) - { - var result = false; - - try - { - #region Check Arguments - - if (CurrentProcess.MainModule == null) - { - Console.WriteLine("Current Process MainModule is null"); - return; - } - - if (args.Length != 3) - { - Console.WriteLine("The program is not user-oriented\n此程序不是面向用户的"); - return; - } - - // arg0 port - if (!int.TryParse(args[0], out var port)) - { - Console.WriteLine("arg0 Port Parse failed"); - return; - } - - var updateExtracted = true; - // arg1 update File/Directory - var updatePath = Path.GetFullPath(args[1]); - if (File.Exists(updatePath)) - { - updateExtracted = false; - } - else if (!Directory.Exists(updatePath)) - { - Console.WriteLine("arg1 update file/directory Not found"); - return; - } - - // arg2 target Directory - string targetPath; - if (!File.Exists(Path.Combine(targetPath = Path.GetFullPath(args[2]), "Netch.exe"))) - { - Console.Write("arg2 Netch Directory doesn't seems right"); - return; - } - - #region if under target Directory,then rerun in temp directory - - if (UpdaterDirectory.StartsWith(targetPath)) - { - // Updater 在目标目录下 - // 将程序复制到临时目录,传递参数 - var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); - var newUpdaterPath = Path.Combine(tempPath, UpdaterFriendlyName); - Directory.CreateDirectory(tempPath); - File.Copy(UpdaterFullName, newUpdaterPath); - Process.Start(new ProcessStartInfo - { - FileName = newUpdaterPath, - Arguments = $"{port} \"{updatePath}\" \"{targetPath}\"", - WorkingDirectory = tempPath, - UseShellExecute = false - }); - result = true; - return; - } - - #endregion - - #endregion - - /*Console.WriteLine("Waiting Attach"); - while (!Debugger.IsAttached) - { - Thread.Sleep(1000); - }*/ - - #region Send Netch Exit command - - Process[] _; - if ((_ = Process.GetProcessesByName("Netch")).Any()) - { - Console.WriteLine("Found Netch process, Send exit command"); - try - { - var udpClient = new UdpClient("127.0.0.1", port); - var sendBytes = Encoding.ASCII.GetBytes("Exit"); - udpClient.Send(sendBytes, sendBytes.Length); - } - catch - { - Console.WriteLine("Send command failed"); - return; - } - - foreach (var proc in _) - { - try - { - proc.WaitForExit(); - } - catch (Exception) - { - // ignored - } - } - } - - #endregion - - var counter = 0; - while (!TestFileFree(Path.Combine(targetPath, "Netch.exe"))) - { - // wait 5 sec - if (counter > 25) - { - Console.WriteLine("Waiting Netch exit timeout"); - return; - } - - Thread.Sleep(200); - counter++; - } - - #region Update - - string extractPath = null; - if (!updateExtracted) - { - extractPath = Path.Combine(UpdaterDirectory, "extract"); - Extract(updatePath, extractPath, true); - - var netchExeFileInfo = FindFile("Netch.exe", extractPath); - - if (netchExeFileInfo == null) - { - throw new Exception("Netch.exe not found in archive!"); - } - - updatePath = netchExeFileInfo.Directory.FullName; - } - - MoveDirectory(updatePath, targetPath, true); - - try - { - if (extractPath != null) - Directory.Delete(extractPath, true); - } - catch - { - // ignored - } - - #endregion - - #region Finished Update,Start Netch - - Console.WriteLine("Start Netch"); - Process.Start(new ProcessStartInfo - { - FileName = Path.Combine(targetPath, "Netch.exe"), - UseShellExecute = true, - }); - - #endregion - - result = true; - } - catch (Exception e) - { - if (e is InvalidDataException) - Console.WriteLine("Archive file Broken"); - Console.WriteLine(e.ToString()); - } - finally - { - if (!result) - { - Console.WriteLine("Press any key to exit..."); - Console.Read(); - } - } - } - - private static void Extract(string archiveFileName, string destDirName, bool overwrite) - { - archiveFileName = Path.GetFullPath(archiveFileName); - destDirName = Path.GetFullPath(destDirName); - - if (!File.Exists("7za.exe")) - File.WriteAllBytes("7za.exe", Resources._7za); - - var argument = new StringBuilder(); - argument.Append($" x \"{archiveFileName}\" -o\"{destDirName}\" "); - if (overwrite) - argument.Append(" -y "); - - Process.Start(new ProcessStartInfo - { - UseShellExecute = false, - WindowStyle = ProcessWindowStyle.Hidden, - FileName = Path.GetFullPath("7za.exe"), - Arguments = argument.ToString() - })?.WaitForExit(); - } - - public static FileInfo FindFile(string filename, string directory) - { - var DirStack = new Stack(); - DirStack.Push(directory); - - while (DirStack.Count > 0) - { - var DirInfo = new DirectoryInfo(DirStack.Pop()); - try - { - foreach (var DirChildInfo in DirInfo.GetDirectories()) - { - DirStack.Push(DirChildInfo.FullName); - } - } - catch - { - // ignored - } - - try - { - foreach (var FileChildInfo in DirInfo.GetFiles()) - { - if (FileChildInfo.Name == filename) - { - return FileChildInfo; - } - } - } - catch - { - // ignored - } - } - - return null; - } - - private static void MoveDirectory(string sourceDirName, string destDirName, bool overwrite) - { - sourceDirName = Path.GetFullPath(sourceDirName); - destDirName = Path.GetFullPath(destDirName); - if (!overwrite) - { - Directory.Move(sourceDirName, destDirName); - } - else - { - var dirStack = new Stack(); - dirStack.Push(sourceDirName); - - while (dirStack.Count > 0) - { - var dirInfo = new DirectoryInfo(dirStack.Pop()); - try - { - foreach (var dirChildInfo in dirInfo.GetDirectories()) - { - var destPath = dirChildInfo.FullName.Replace(sourceDirName, destDirName); - try - { - if (!Directory.Exists(destPath)) - { - Directory.CreateDirectory(destPath); - } - } - catch (Exception e) - { - Console.WriteLine($"Create {destPath} Folder Error: {e.Message}"); - } - - dirStack.Push(dirChildInfo.FullName); - } - - foreach (var fileChildInfo in dirInfo.GetFiles()) - { - var destPath = fileChildInfo.FullName.Replace(sourceDirName, destDirName); - try - { - if (File.Exists(destPath)) - { - File.Delete(destPath); - } - - fileChildInfo.MoveTo(destPath); - } - catch (Exception e) - { - Console.WriteLine($"Move {fileChildInfo.FullName} To {destPath} Error: {e.Message}"); - } - } - } - catch (Exception e) - { - Console.WriteLine($"ERROR:{e.Message}"); - } - } - } - } - - private static bool TestFileFree(string FileName) - { - try - { - File.Move(FileName, FileName); - return true; - } - catch - { - return false; - } - } - } -} \ No newline at end of file diff --git a/NetchUpdater/Properties/AssemblyInfo.cs b/NetchUpdater/Properties/AssemblyInfo.cs deleted file mode 100644 index 746c1612..00000000 --- a/NetchUpdater/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("NetchUpdater")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("NetchUpdater")] -[assembly: AssemblyCopyright("Copyright © 2020")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("828318A8-9B90-4A5F-BD6B-E632CC9D8933")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/NetchUpdater/Properties/Resources.Designer.cs b/NetchUpdater/Properties/Resources.Designer.cs deleted file mode 100644 index 0e972568..00000000 --- a/NetchUpdater/Properties/Resources.Designer.cs +++ /dev/null @@ -1,73 +0,0 @@ -//------------------------------------------------------------------------------ -// -// 此代码由工具生成。 -// 运行时版本:4.0.30319.42000 -// -// 对此文件的更改可能会导致不正确的行为,并且如果 -// 重新生成代码,这些更改将会丢失。 -// -//------------------------------------------------------------------------------ - -namespace NetchUpdater.Properties { - using System; - - - /// - /// 一个强类型的资源类,用于查找本地化的字符串等。 - /// - // 此类是由 StronglyTypedResourceBuilder - // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 - // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen - // (以 /str 作为命令选项),或重新生成 VS 项目。 - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - /// - /// 返回此类使用的缓存的 ResourceManager 实例。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("NetchUpdater.Properties.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// 重写当前线程的 CurrentUICulture 属性 - /// 重写当前线程的 CurrentUICulture 属性。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// 查找 System.Byte[] 类型的本地化资源。 - /// - internal static byte[] _7za { - get { - object obj = ResourceManager.GetObject("7za", resourceCulture); - return ((byte[])(obj)); - } - } - } -} diff --git a/NetchUpdater/Properties/Resources.resx b/NetchUpdater/Properties/Resources.resx deleted file mode 100644 index 628c95de..00000000 --- a/NetchUpdater/Properties/Resources.resx +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - - ..\Resources\7za.exe;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/NetchUpdater/Resources/7za.exe b/NetchUpdater/Resources/7za.exe deleted file mode 100644 index 2bdd57d2..00000000 Binary files a/NetchUpdater/Resources/7za.exe and /dev/null differ