diff --git a/v2rayN/ServiceLib/Common/ProcUtils.cs b/v2rayN/ServiceLib/Common/ProcUtils.cs new file mode 100644 index 00000000..65027a77 --- /dev/null +++ b/v2rayN/ServiceLib/Common/ProcUtils.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 5985fa9f..10a5302e 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -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 GetSystemHosts() { var systemHosts = new Dictionary(); diff --git a/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs b/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs index c0cfb32c..df84eef7 100644 --- a/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs @@ -103,7 +103,7 @@ namespace ServiceLib.ViewModels address = Utils.GetConfigPath(address); if (File.Exists(address)) { - Utils.ProcessStart(address); + ProcUtils.ProcessStart(address); } else { diff --git a/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs b/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs index 77230770..947bac03 100644 --- a/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs @@ -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 { diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index e8def23f..b052b6c7 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -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); } } diff --git a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs index 56fed37d..55a12ac0 100644 --- a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs @@ -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/"); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index 52c0fdc9..e73fda5d 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -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()); } } diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs index 3c2c4508..732ae00e 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs @@ -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"); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs index deeba10d..d38f185f 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs @@ -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"); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs index 15f5dd2f..749feebc 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs @@ -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) diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs index 290021e0..56a8242d 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs @@ -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/"); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index c2e745f3..d91656a3 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -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()); } } diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs index 7262e2b7..5b6cca4c 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs @@ -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"); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs index 64342617..4340b2f3 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs @@ -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"); } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs index 37b1c38a..39086ec4 100644 --- a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs @@ -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)