Skip to content

Commit afba4e2

Browse files
committed
Fixed crash if GTK not installed
1 parent 7d9c996 commit afba4e2

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

NativeFileDialog.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ static NativeFileDialog()
2424

2525
foreach (Type nativeProvider in nativeProviders)
2626
{
27-
if (Activator.CreateInstance(nativeProvider) is INativeDialogProvider provider)
27+
try
2828
{
29-
if (!provider.CurrentPlatformSupported)
30-
continue;
31-
32-
if (_provider is null || _provider.Priority < provider.Priority)
33-
_provider = provider;
29+
if (Activator.CreateInstance(nativeProvider) is INativeDialogProvider provider)
30+
{
31+
if (!provider.CurrentPlatformSupported)
32+
continue;
33+
34+
if (_provider is null || _provider.Priority < provider.Priority)
35+
_provider = provider;
36+
}
3437
}
38+
catch { }
3539
}
3640
}
3741

NativeProviders/GTKDialogProvider.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Gdk;
22
using Gtk;
33
using System;
4+
using System.Diagnostics;
45

56
namespace SharpFileDialog.NativeProviders
67
{
@@ -100,11 +101,21 @@ static void ThrowOnPlatformUnsupported()
100101

101102
static bool InitCheck()
102103
{
103-
string[] args = Environment.GetCommandLineArgs();
104-
string name = args.Length > 0 ? args[0] : string.Empty;
105-
string[] gtkArgs = Array.Empty<string>();
104+
try
105+
{
106+
string[] args = Environment.GetCommandLineArgs();
107+
string name = args.Length > 0 ? args[0] : string.Empty;
108+
string[] gtkArgs = Array.Empty<string>();
106109

107-
return Application.InitCheck(name, ref gtkArgs);
110+
return Application.InitCheck(name, ref gtkArgs);
111+
}
112+
catch (Exception e)
113+
{
114+
#if DEBUG
115+
Debug.WriteLine(e);
116+
#endif
117+
return false;
118+
}
108119
}
109120

110121
static void SetDefaultPath(IFileChooser widget, string path)

0 commit comments

Comments
 (0)