diff --git a/v2rayN/v2rayUpgrade/MainForm.cs b/v2rayN/v2rayUpgrade/MainForm.cs index 6b9ff16c..fe2ee592 100644 --- a/v2rayN/v2rayUpgrade/MainForm.cs +++ b/v2rayN/v2rayUpgrade/MainForm.cs @@ -11,18 +11,22 @@ namespace v2rayUpgrade public partial class MainForm : Form { private readonly string defaultFilename = "v2ray-windows.zip"; - private string fileName; + private string? fileName; public MainForm(string[] args) { InitializeComponent(); if (args.Length > 0) { - fileName = string.Join(" ", args); - fileName = HttpUtility.UrlDecode(fileName); + fileName = HttpUtility.UrlDecode(string.Join(" ", args)); + } + else + { + fileName = defaultFilename; } } - private void showWarn(string message) + + private void ShowWarn(string message) { MessageBox.Show(message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning); } @@ -34,7 +38,7 @@ namespace v2rayUpgrade Process[] existing = Process.GetProcessesByName("v2rayN"); foreach (Process p in existing) { - string path = p.MainModule.FileName; + string? path = p.MainModule?.FileName; if (path == GetPath("v2rayN.exe")) { p.Kill(); @@ -45,72 +49,67 @@ namespace v2rayUpgrade catch (Exception ex) { // Access may be denied without admin right. The user may not be an administrator. - showWarn("Failed to close v2rayN(关闭v2rayN失败).\n" + + ShowWarn("Failed to close v2rayN(关闭v2rayN失败).\n" + "Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN,否则可能升级失败。\n\n" + ex.StackTrace); } - StringBuilder sb = new StringBuilder(); - try + if (!File.Exists(fileName)) { - if (!File.Exists(fileName)) + if (File.Exists(defaultFilename)) { - if (File.Exists(defaultFilename)) - { - fileName = defaultFilename; - } - else - { - showWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); - return; - } + fileName = defaultFilename; } + else + { + ShowWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); + return; + } + } - string thisAppOldFile = Application.ExecutablePath + ".tmp"; + StringBuilder sb = new(); + try + { + string thisAppOldFile = $"{Application.ExecutablePath}.tmp"; File.Delete(thisAppOldFile); string startKey = "v2rayN/"; - - using (ZipArchive archive = ZipFile.OpenRead(fileName)) + using ZipArchive archive = ZipFile.OpenRead(fileName); + foreach (ZipArchiveEntry entry in archive.Entries) { - foreach (ZipArchiveEntry entry in archive.Entries) + try { - try + if (entry.Length == 0) { - if (entry.Length == 0) - { - continue; - } - string fullName = entry.FullName; - if (fullName.StartsWith(startKey)) - { - fullName = fullName.Substring(startKey.Length, fullName.Length - startKey.Length); - } - if (Application.ExecutablePath.ToLower() == GetPath(fullName).ToLower()) - { - File.Move(Application.ExecutablePath, thisAppOldFile); - } - - string entryOuputPath = GetPath(fullName); - - FileInfo fileInfo = new FileInfo(entryOuputPath); - fileInfo.Directory.Create(); - entry.ExtractToFile(entryOuputPath, true); + continue; } - catch (Exception ex) + string fullName = entry.FullName; + if (fullName.StartsWith(startKey)) { - sb.Append(ex.StackTrace); + fullName = fullName[startKey.Length..]; } + if (string.Equals(Application.ExecutablePath, GetPath(fullName), StringComparison.OrdinalIgnoreCase)) + { + File.Move(Application.ExecutablePath, thisAppOldFile); + } + + string entryOuputPath = GetPath(fullName); + Directory.CreateDirectory(Path.GetDirectoryName(entryOuputPath) ?? ""); + entry.ExtractToFile(entryOuputPath, true); + } + catch (Exception ex) + { + sb.Append(ex.StackTrace); } } } catch (Exception ex) { - showWarn("Upgrade Failed(升级失败)." + ex.StackTrace); + ShowWarn("Upgrade Failed(升级失败)." + ex.StackTrace); return; } if (sb.Length > 0) { - showWarn("Upgrade Failed,Hold ctrl + c to copy to clipboard.\n" + + ShowWarn("Upgrade Failed,Hold ctrl + c to copy to clipboard.\n" + "(升级失败,按住ctrl+c可以复制到剪贴板)." + sb.ToString()); return; } diff --git a/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj b/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj index 835420ba..40ddb328 100644 --- a/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj +++ b/v2rayN/v2rayUpgrade/v2rayUpgrade.csproj @@ -6,5 +6,6 @@ Copyright © 2019-2023 (GPLv3) 1.1.0.0 app.manifest + enable \ No newline at end of file