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,72 +49,67 @@ 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(); if (!File.Exists(fileName))
try
{ {
if (!File.Exists(fileName)) if (File.Exists(defaultFilename))
{ {
if (File.Exists(defaultFilename)) fileName = defaultFilename;
{
fileName = defaultFilename;
}
else
{
showWarn("Upgrade Failed, File Not Exist(升级失败,文件不存在).");
return;
}
} }
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); 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 if (entry.Length == 0)
{ {
if (entry.Length == 0) continue;
{
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);
} }
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) 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