mirror of
https://github.com/2dust/v2rayN.git
synced 2026-10-20 12:15:00 +03:00
Refactor ProcessStart RebootAsAdmin
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ServiceLib.Common;
|
||||
|
||||
public static class ProcUtils
|
||||
{
|
||||
private static readonly string _tag = "ProcUtils";
|
||||
|
||||
public static void ProcessStart(string? fileName, string arguments = "")
|
||||
{
|
||||
ProcessStart(fileName, arguments, null);
|
||||
}
|
||||
|
||||
public static int? ProcessStart(string? fileName, string arguments, string? dir)
|
||||
{
|
||||
if (fileName.IsNullOrEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (fileName.Contains(' ')) fileName = fileName.AppendQuotes();
|
||||
if (arguments.Contains(' ')) arguments = arguments.AppendQuotes();
|
||||
|
||||
Process process = new()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = true,
|
||||
FileName = fileName,
|
||||
Arguments = arguments,
|
||||
WorkingDirectory = dir
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
return process.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RebootAsAdmin(bool blAdmin = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessStartInfo startInfo = new()
|
||||
{
|
||||
UseShellExecute = true,
|
||||
Arguments = Global.RebootAs,
|
||||
WorkingDirectory = Utils.StartupPath(),
|
||||
FileName = Utils.GetExePath().AppendQuotes(),
|
||||
Verb = blAdmin ? "runas" : null,
|
||||
};
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -591,26 +591,6 @@ namespace ServiceLib.Common
|
||||
return Guid.TryParse(strSrc, out _);
|
||||
}
|
||||
|
||||
public static void ProcessStart(string? fileName, string arguments = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
if (fileName.IsNullOrEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (fileName.Contains(' ')) fileName = fileName.AppendQuotes();
|
||||
if (arguments.Contains(' ')) arguments = arguments.AppendQuotes();
|
||||
|
||||
Process.Start(new ProcessStartInfo(fileName, arguments) { UseShellExecute = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, string> GetSystemHosts()
|
||||
{
|
||||
var systemHosts = new Dictionary<string, string>();
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace ServiceLib.ViewModels
|
||||
address = Utils.GetConfigPath(address);
|
||||
if (File.Exists(address))
|
||||
{
|
||||
Utils.ProcessStart(address);
|
||||
ProcUtils.ProcessStart(address);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -130,11 +130,6 @@ namespace ServiceLib.ViewModels
|
||||
DisplayOperationMsg(ResUI.LocalRestoreInvalidZipTips);
|
||||
return;
|
||||
}
|
||||
if (!Utils.UpgradeAppExists(out _))
|
||||
{
|
||||
DisplayOperationMsg(ResUI.UpgradeAppNotExistTip);
|
||||
return;
|
||||
}
|
||||
|
||||
//backup first
|
||||
var fileBackup = Utils.GetBackupPath(BackupFileName);
|
||||
@@ -150,12 +145,9 @@ namespace ServiceLib.ViewModels
|
||||
|
||||
if (Utils.IsWindows())
|
||||
{
|
||||
service?.RebootAsAdmin(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
service?.Shutdown();
|
||||
ProcUtils.RebootAsAdmin(false);
|
||||
}
|
||||
service?.Shutdown();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using ReactiveUI;
|
||||
using ReactiveUI.Fody.Helpers;
|
||||
using Splat;
|
||||
using System.Diagnostics;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
namespace ServiceLib.ViewModels
|
||||
{
|
||||
@@ -319,20 +317,9 @@ namespace ServiceLib.ViewModels
|
||||
return;
|
||||
}
|
||||
|
||||
Process process = new()
|
||||
var id = ProcUtils.ProcessStart(fileName, arg, Utils.StartupPath());
|
||||
if (id > 0)
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = true,
|
||||
FileName = fileName,
|
||||
Arguments = arg.AppendQuotes(),
|
||||
WorkingDirectory = Utils.StartupPath()
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
if (process.Id > 0)
|
||||
{
|
||||
await MyAppExitAsync(false);
|
||||
await MyAppExitAsync(false);
|
||||
}
|
||||
}
|
||||
@@ -513,22 +500,10 @@ namespace ServiceLib.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RebootAsAdmin(bool blAdmin = true)
|
||||
public async Task RebootAsAdmin()
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessStartInfo startInfo = new()
|
||||
{
|
||||
UseShellExecute = true,
|
||||
Arguments = Global.RebootAs,
|
||||
WorkingDirectory = Utils.StartupPath(),
|
||||
FileName = Utils.GetExePath().AppendQuotes(),
|
||||
Verb = blAdmin ? "runas" : null,
|
||||
};
|
||||
Process.Start(startInfo);
|
||||
await MyAppExitAsync(false);
|
||||
}
|
||||
catch { }
|
||||
ProcUtils.RebootAsAdmin();
|
||||
await MyAppExitAsync(false);
|
||||
}
|
||||
|
||||
private async Task ClearServerStatistics()
|
||||
@@ -542,15 +517,15 @@ namespace ServiceLib.ViewModels
|
||||
var path = Utils.StartupPath();
|
||||
if (Utils.IsWindows())
|
||||
{
|
||||
Utils.ProcessStart(path);
|
||||
ProcUtils.ProcessStart(path);
|
||||
}
|
||||
else if (Utils.IsLinux())
|
||||
{
|
||||
Utils.ProcessStart("nautilus", path);
|
||||
ProcUtils.ProcessStart("nautilus", path);
|
||||
}
|
||||
else if (Utils.IsOSX())
|
||||
{
|
||||
Utils.ProcessStart("open", path);
|
||||
ProcUtils.ProcessStart("open", path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user