Refactor ProcessStart RebootAsAdmin

pull/6443/head
2dust 2025-01-03 20:56:51 +08:00
parent 3db1dd7bbb
commit c0d27504ac
15 changed files with 93 additions and 82 deletions

View File

@ -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);
}
}
}

View File

@ -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>();

View File

@ -103,7 +103,7 @@ namespace ServiceLib.ViewModels
address = Utils.GetConfigPath(address);
if (File.Exists(address))
{
Utils.ProcessStart(address);
ProcUtils.ProcessStart(address);
}
else
{

View File

@ -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
{

View File

@ -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);
}
}

View File

@ -65,12 +65,12 @@ namespace v2rayN.Desktop.Views
private void linkDnsObjectDoc_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://xtls.github.io/config/dns.html#dnsobject");
ProcUtils.ProcessStart("https://xtls.github.io/config/dns.html#dnsobject");
}
private void linkDnsSingboxObjectDoc_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/dns/");
ProcUtils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/dns/");
}
}
}

View File

@ -330,12 +330,12 @@ namespace v2rayN.Desktop.Views
private void menuPromotion_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart($"{Utils.Base64Decode(Global.PromotionUrl)}?t={DateTime.Now.Ticks}");
ProcUtils.ProcessStart($"{Utils.Base64Decode(Global.PromotionUrl)}?t={DateTime.Now.Ticks}");
}
private void menuSettingsSetUWP_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart(Utils.GetBinPath("EnableLoopback.exe"));
ProcUtils.ProcessStart(Utils.GetBinPath("EnableLoopback.exe"));
}
public async Task ScanScreenTaskAsync()
@ -481,7 +481,7 @@ namespace v2rayN.Desktop.Views
{
if (sender is MenuItem item)
{
Utils.ProcessStart(item.Tag?.ToString());
ProcUtils.ProcessStart(item.Tag?.ToString());
}
}

View File

@ -95,7 +95,7 @@ namespace v2rayN.Desktop.Views
private void linkRuleobjectDoc_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://xtls.github.io/config/routing.html#ruleobject");
ProcUtils.ProcessStart("https://xtls.github.io/config/routing.html#ruleobject");
}
}
}

View File

@ -203,7 +203,7 @@ namespace v2rayN.Desktop.Views
private void linkCustomRulesetPath4Singbox(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://github.com/2dust/v2rayCustomRoutingList/blob/master/singbox_custom_ruleset_example.json");
ProcUtils.ProcessStart("https://github.com/2dust/v2rayCustomRoutingList/blob/master/singbox_custom_ruleset_example.json");
}
}
}

View File

@ -117,12 +117,12 @@ namespace v2rayN.Desktop.Views
private void linkdomainStrategy_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://xtls.github.io/config/routing.html");
ProcUtils.ProcessStart("https://xtls.github.io/config/routing.html");
}
private void linkdomainStrategy4Singbox_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/shared/listen/#domain_strategy");
ProcUtils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/shared/listen/#domain_strategy");
}
private void btnCancel_Click(object? sender, RoutedEventArgs e)

View File

@ -66,12 +66,12 @@ namespace v2rayN.Views
private void linkDnsObjectDoc_Click(object sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://xtls.github.io/config/dns.html#dnsobject");
ProcUtils.ProcessStart("https://xtls.github.io/config/dns.html#dnsobject");
}
private void linkDnsSingboxObjectDoc_Click(object sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/dns/");
ProcUtils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/dns/");
}
}
}

View File

@ -308,12 +308,12 @@ namespace v2rayN.Views
private void menuPromotion_Click(object sender, RoutedEventArgs e)
{
Utils.ProcessStart($"{Utils.Base64Decode(Global.PromotionUrl)}?t={DateTime.Now.Ticks}");
ProcUtils.ProcessStart($"{Utils.Base64Decode(Global.PromotionUrl)}?t={DateTime.Now.Ticks}");
}
private void menuSettingsSetUWP_Click(object sender, RoutedEventArgs e)
{
Utils.ProcessStart(Utils.GetBinPath("EnableLoopback.exe"));
ProcUtils.ProcessStart(Utils.GetBinPath("EnableLoopback.exe"));
}
private async Task ScanScreenTaskAsync()
@ -443,7 +443,7 @@ namespace v2rayN.Views
{
if (sender is MenuItem item)
{
Utils.ProcessStart(item.Tag.ToString());
ProcUtils.ProcessStart(item.Tag.ToString());
}
}

View File

@ -89,7 +89,7 @@ namespace v2rayN.Views
private void linkRuleobjectDoc_Click(object sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://xtls.github.io/config/routing.html#ruleobject");
ProcUtils.ProcessStart("https://xtls.github.io/config/routing.html#ruleobject");
}
}
}

View File

@ -197,7 +197,7 @@ namespace v2rayN.Views
private void linkCustomRulesetPath4Singbox(object sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://github.com/2dust/v2rayCustomRoutingList/blob/master/singbox_custom_ruleset_example.json");
ProcUtils.ProcessStart("https://github.com/2dust/v2rayCustomRoutingList/blob/master/singbox_custom_ruleset_example.json");
}
}
}

View File

@ -122,12 +122,12 @@ namespace v2rayN.Views
private void linkdomainStrategy_Click(object sender, System.Windows.RoutedEventArgs e)
{
Utils.ProcessStart("https://xtls.github.io/config/routing.html");
ProcUtils.ProcessStart("https://xtls.github.io/config/routing.html");
}
private void linkdomainStrategy4Singbox_Click(object sender, RoutedEventArgs e)
{
Utils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/shared/listen/#domain_strategy");
ProcUtils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/shared/listen/#domain_strategy");
}
private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)