Update v2rayUpgrade

pull/3311/head
小仙女 2 years ago
parent fe070b306f
commit 527bfec24c

@ -11,18 +11,22 @@ namespace v2rayUpgrade
public partial class MainForm : Form public partial class MainForm : Form
{ {
private readonly string defaultFilename = "v2ray-windows.zip"; private readonly string defaultFilename = "v2ray-windows.zip";
private string fileName; private string? fileName;
public MainForm(string[] args) public MainForm(string[] args)
{ {
InitializeComponent(); InitializeComponent();
if (args.Length > 0) if (args.Length > 0)
{ {
fileName = string.Join(" ", args); fileName = HttpUtility.UrlDecode(string.Join(" ", args));
fileName = HttpUtility.UrlDecode(fileName); }
else
{
fileName = defaultFilename;
} }
} }
private void showWarn(string message)
private void ShowWarn(string message)
{ {
MessageBox.Show(message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning); MessageBox.Show(message, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
} }
@ -34,7 +38,7 @@ namespace v2rayUpgrade
Process[] existing = Process.GetProcessesByName("v2rayN"); Process[] existing = Process.GetProcessesByName("v2rayN");
foreach (Process p in existing) foreach (Process p in existing)
{ {
string path = p.MainModule.FileName; string? path = p.MainModule?.FileName;
if (path == GetPath("v2rayN.exe")) if (path == GetPath("v2rayN.exe"))
{ {
p.Kill(); p.Kill();
@ -45,13 +49,10 @@ namespace v2rayUpgrade
catch (Exception ex) catch (Exception ex)
{ {
// Access may be denied without admin right. The user may not be an administrator. // 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); "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))
@ -60,18 +61,19 @@ namespace v2rayUpgrade
} }
else else
{ {
showWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在)."); ShowWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在).");
return; return;
} }
} }
string thisAppOldFile = Application.ExecutablePath + ".tmp"; StringBuilder sb = new();
try
{
string thisAppOldFile = $"{Application.ExecutablePath}.tmp";
File.Delete(thisAppOldFile); File.Delete(thisAppOldFile);
string startKey = "v2rayN/"; 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
@ -83,17 +85,15 @@ namespace v2rayUpgrade
string fullName = entry.FullName; string fullName = entry.FullName;
if (fullName.StartsWith(startKey)) if (fullName.StartsWith(startKey))
{ {
fullName = fullName.Substring(startKey.Length, fullName.Length - startKey.Length); fullName = fullName[startKey.Length..];
} }
if (Application.ExecutablePath.ToLower() == GetPath(fullName).ToLower()) if (string.Equals(Application.ExecutablePath, GetPath(fullName), StringComparison.OrdinalIgnoreCase))
{ {
File.Move(Application.ExecutablePath, thisAppOldFile); File.Move(Application.ExecutablePath, thisAppOldFile);
} }
string entryOuputPath = GetPath(fullName); string entryOuputPath = GetPath(fullName);
Directory.CreateDirectory(Path.GetDirectoryName(entryOuputPath) ?? "");
FileInfo fileInfo = new FileInfo(entryOuputPath);
fileInfo.Directory.Create();
entry.ExtractToFile(entryOuputPath, true); entry.ExtractToFile(entryOuputPath, true);
} }
catch (Exception ex) catch (Exception ex)
@ -102,15 +102,14 @@ namespace v2rayUpgrade
} }
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
showWarn("Upgrade Failed(升级失败)." + ex.StackTrace); ShowWarn("Upgrade Failed(升级失败)." + ex.StackTrace);
return; return;
} }
if (sb.Length > 0) 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()); "(升级失败,按住ctrl+c可以复制到剪贴板)." + sb.ToString());
return; return;
} }

@ -6,5 +6,6 @@
<Copyright>Copyright © 2019-2023 (GPLv3)</Copyright> <Copyright>Copyright © 2019-2023 (GPLv3)</Copyright>
<FileVersion>1.1.0.0</FileVersion> <FileVersion>1.1.0.0</FileVersion>
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
</Project> </Project>
Loading…
Cancel
Save