-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeFileDialog.cs
More file actions
109 lines (89 loc) · 3.58 KB
/
NativeFileDialog.cs
File metadata and controls
109 lines (89 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// .NET Standard 2.1 OR >= .NET Core 3.0
#if (!NETSTANDARD2_0 && NETSTANDARD2_0_OR_GREATER) || NETCOREAPP3_0_OR_GREATER
#define USE_NOTNULLWHEN
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
namespace SharpFileDialog
{
public static class NativeFileDialog
{
public static bool CurrentPlatformSupported => Provider is not null;
public static INativeDialogProvider? Provider { get; set; }
static bool _providerSearched = false;
public static bool SetDefaultProvider()
{
_providerSearched = true;
var previousProvider = Provider;
Assembly currentAssembly = Assembly.GetExecutingAssembly();
IEnumerable<Type> nativeProviders = currentAssembly.GetTypes().Where(type => type.GetInterface(nameof(INativeDialogProvider)) is not null);
foreach (Type nativeProvider in nativeProviders)
{
try
{
if (Activator.CreateInstance(nativeProvider) is INativeDialogProvider provider)
{
if (!provider.CurrentPlatformSupported)
continue;
if (Provider is null || Provider.Priority < provider.Priority)
Provider = provider;
}
}
catch { }
}
return Provider != previousProvider;
}
#if USE_NOTNULLWHEN
public static bool OpenDialog(Filter[]? filters, string? defaultPath, [NotNullWhen(true)] out string? outPath)
#else
public static bool OpenDialog(Filter[]? filters, string? defaultPath, out string? outPath)
#endif
{
INativeDialogProvider provider = EnsureProviderAvailable();
return provider.OpenDialog(filters, defaultPath, out outPath);
}
#if USE_NOTNULLWHEN
public static bool OpenDialogMultiple(Filter[]? filters, string? defaultPath, [NotNullWhen(true)] out string[]? outPaths)
#else
public static bool OpenDialogMultiple(Filter[]? filters, string? defaultPath, out string[]? outPaths)
#endif
{
INativeDialogProvider provider = EnsureProviderAvailable();
return provider.OpenDialogMultiple(filters, defaultPath, out outPaths);
}
#if USE_NOTNULLWHEN
public static bool SaveDialog(Filter[]? filters, string? defaultPath, [NotNullWhen(true)] out string? outPath)
#else
public static bool SaveDialog(Filter[]? filters, string? defaultPath, out string? outPath)
#endif
{
INativeDialogProvider provider = EnsureProviderAvailable();
return provider.SaveDialog(filters, defaultPath, out outPath);
}
#if USE_NOTNULLWHEN
public static bool PickFolder(string? defaultPath, [NotNullWhen(true)] out string? outPath)
#else
public static bool PickFolder(string? defaultPath, out string? outPath)
#endif
{
INativeDialogProvider provider = EnsureProviderAvailable();
return provider.PickFolder(defaultPath, out outPath);
}
static INativeDialogProvider EnsureProviderAvailable()
{
if (!_providerSearched)
SetDefaultProvider();
if (Provider is null)
throw new PlatformNotSupportedException("Dialog provider for current platform is not available!");
return Provider;
}
public struct Filter
{
public string Name;
public string[] Extensions;
}
}
}