From a133b624575b7a966e7bb5262d6851c4df1f99ea Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Tue, 12 Sep 2023 13:22:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=201.23.10912.11322?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BD.Common/Diagnostics/Process2.cs | 63 +++++++++++++++++++-------- src/Directory.Build.props | 2 +- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/BD.Common/Diagnostics/Process2.cs b/src/BD.Common/Diagnostics/Process2.cs index 6d7bee426..16cce1fa3 100644 --- a/src/BD.Common/Diagnostics/Process2.cs +++ b/src/BD.Common/Diagnostics/Process2.cs @@ -22,6 +22,7 @@ public static class Process2 /// 或 参数的长度与该进程的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可能为以下消息之一:“传递到系统调用的数据区域太小。” 或“拒绝访问。” /// /// 不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法 + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Process? StartPath(string fileName, string path) { if (OperatingSystem.IsMacOS()) @@ -43,25 +44,9 @@ public static class Process2 } } - /// - /// 通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 Process 组件相关联 - /// - /// 要在进程中运行的应用程序文件的名称 - /// 启动该进程时传递的命令行参数 - /// 获取或设置指示是否使用操作系统 shell 启动进程的值 - /// 当 useShellExecute 属性为 时,将获取或设置要启动的进程的工作目录。 当 useShellExecute 为 时,获取或设置包含要启动的进程的目录注意:当 UseShellExecute 为 时,是包含要启动的进程的目录的完全限定名 - /// - /// - /// 已释放此进程对象 - /// - /// 打开关联的文件时出错 - /// 或 fileName 中指定的文件未找到 - /// 或 参数的长度与该进程的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可能为以下消息之一:“传递到系统调用的数据区域太小。” 或“拒绝访问。” - /// - /// 不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法 - public static Process? Start(string fileName, string? arguments = null, bool useShellExecute = false, string? workingDirectory = null, IReadOnlyDictionary? environment = null) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static ProcessStartInfo GetProcessStartInfo(string fileName, string? arguments = null, bool useShellExecute = false, string? workingDirectory = null, IReadOnlyDictionary? environment = null) { - if (string.IsNullOrEmpty(fileName)) return null; var p = new ProcessStartInfo(fileName); if (!string.IsNullOrEmpty(arguments)) { @@ -87,9 +72,48 @@ public static class Process2 } } p.UseShellExecute = useShellExecute; - return Process.Start(p); + + return p; + } + + /// + /// 通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 Process 组件相关联 + /// + /// 要在进程中运行的应用程序文件的名称 + /// 启动该进程时传递的命令行参数 + /// 获取或设置指示是否使用操作系统 shell 启动进程的值 + /// 当 useShellExecute 属性为 时,将获取或设置要启动的进程的工作目录。 当 useShellExecute 为 时,获取或设置包含要启动的进程的目录注意:当 UseShellExecute 为 时,是包含要启动的进程的目录的完全限定名 + /// + /// + /// 已释放此进程对象 + /// + /// 打开关联的文件时出错 + /// 或 fileName 中指定的文件未找到 + /// 或 参数的长度与该进程的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可能为以下消息之一:“传递到系统调用的数据区域太小。” 或“拒绝访问。” + /// + /// 不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法 + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Process? Start(string fileName, string? arguments = null, bool useShellExecute = false, string? workingDirectory = null, IReadOnlyDictionary? environment = null) + { + if (string.IsNullOrEmpty(fileName)) return null; + var p = GetProcessStartInfo(fileName, arguments, useShellExecute, workingDirectory, environment); + try + { + return Process.Start(p); + } + catch (Win32Exception) + { + if (!p.UseShellExecute) + { + // System.ComponentModel.Win32Exception: An error occurred trying to start process 'a.exe' with working directory 'path'. 请求的操作需要提升。 + p = GetProcessStartInfo(fileName, arguments, true, workingDirectory: null, environment); + return Process.Start(p); + } + } + return null; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool OpenCoreByProcess(string url, Action? onError = null) { try @@ -106,6 +130,7 @@ public static bool OpenCoreByProcess(string url, Action? onError = null) } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string RunShell(string fileName, string value, Action? onError = null) { try diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 559e016b8..22f6936ec 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -10,7 +10,7 @@ enable true - 1.23.10831.10300 + 1.23.10912.11322 https://avatars.githubusercontent.com/u/79355691?s=200&v=4 江苏蒸汽凡星科技有限公司 ©️ $(Company). All rights reserved.