From 176b2b892d67f59187aab915d84ff17377e37a63 Mon Sep 17 00:00:00 2001 From: Misaka_L Date: Tue, 7 Jul 2026 01:30:21 +0800 Subject: [PATCH] Fix `BclLauncher.Exec` throw exception instead of return false (#21704) * Fix BclLauncher throw exception instead of return false when Process.Start thrown * Add logging message when BclLauncher.Exec fail --- .../Platform/Storage/FileIO/BclLauncher.cs | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs b/src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs index da4cb01df5..a380be57e2 100644 --- a/src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs +++ b/src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Text.RegularExpressions; using System.Threading.Tasks; +using Avalonia.Logging; namespace Avalonia.Platform.Storage.FileIO; @@ -38,31 +39,40 @@ internal class BclLauncher : ILauncher private static bool Exec(string urlOrFile) { - if (OperatingSystem.IsLinux()) + try { - // If no associated application/json MimeType is found xdg-open opens return error - // but it tries to open it anyway using the console editor (nano, vim, other..) - var args = EscapeForShell(urlOrFile); - ShellExecRaw($"xdg-open \\\"{args}\\\"", waitForExit: false); - return true; - } - else if (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) - { - var info = new ProcessStartInfo + if (OperatingSystem.IsLinux()) + { + // If no associated application/json MimeType is found xdg-open opens return error + // but it tries to open it anyway using the console editor (nano, vim, other..) + var args = EscapeForShell(urlOrFile); + ShellExecRaw($"xdg-open \\\"{args}\\\"", waitForExit: false); + return true; + } + else if (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) { - FileName = OperatingSystem.IsWindows() ? urlOrFile : "open", - CreateNoWindow = true, - UseShellExecute = OperatingSystem.IsWindows() - }; - // Using the argument list avoids having to escape spaces and other special - // characters that are part of valid macos file and folder paths. - if (OperatingSystem.IsMacOS()) - info.ArgumentList.Add(urlOrFile); - using var process = Process.Start(info); - return true; + var info = new ProcessStartInfo + { + FileName = OperatingSystem.IsWindows() ? urlOrFile : "open", + CreateNoWindow = true, + UseShellExecute = OperatingSystem.IsWindows() + }; + // Using the argument list avoids having to escape spaces and other special + // characters that are part of valid macos file and folder paths. + if (OperatingSystem.IsMacOS()) + info.ArgumentList.Add(urlOrFile); + using var process = Process.Start(info); + return true; + } + else + { + return false; + } } - else + catch(Exception ex) { + Logger.TryGet(LogEventLevel.Error, nameof(BclLauncher))? + .Log(null, "Exception during BclLauncher.Exec: {Error}", ex); return false; } }