Browse Source

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
pull/21728/head
Misaka_L 3 months ago
committed by GitHub
parent
commit
176b2b892d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 52
      src/Avalonia.Base/Platform/Storage/FileIO/BclLauncher.cs

52
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;
}
}

Loading…
Cancel
Save