- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeFileDialog.cs
More file actions
Latest commit
109 lines (89 loc) · 3.58 KB
/
Copy pathNativeFileDialog.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
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics.CodeAnalysis;
usingSystem.Linq;
usingSystem.Reflection;
namespaceSharpFileDialog
{
publicstaticclassNativeFileDialog
{
publicstaticboolCurrentPlatformSupported=>Provideris not null;
publicstaticINativeDialogProvider?Provider{get;set;}
staticbool_providerSearched=false;
publicstaticboolSetDefaultProvider()
{
_providerSearched=true;
varpreviousProvider=Provider;
AssemblycurrentAssembly=Assembly.GetExecutingAssembly();
IEnumerable<Type>nativeProviders=currentAssembly.GetTypes().Where(type =>type.GetInterface(nameof(INativeDialogProvider))is not null);
foreach(TypenativeProviderinnativeProviders)
{
try
{
if(Activator.CreateInstance(nativeProvider)isINativeDialogProviderprovider)
{
if(!provider.CurrentPlatformSupported)
continue;
if(Providerisnull||Provider.Priority<provider.Priority)
Provider=provider;
}
}
catch{}
}
returnProvider!=previousProvider;
}
#if USE_NOTNULLWHEN
publicstaticboolOpenDialog(Filter[]?filters,string?defaultPath,[NotNullWhen(true)]outstring?outPath)
#else
publicstaticboolOpenDialog(Filter[]?filters,string?defaultPath,outstring?outPath)
#endif
{
INativeDialogProviderprovider=EnsureProviderAvailable();
returnprovider.OpenDialog(filters,defaultPath,outoutPath);
}
#if USE_NOTNULLWHEN
publicstaticboolOpenDialogMultiple(Filter[]?filters,string?defaultPath,[NotNullWhen(true)]outstring[]?outPaths)
#else
publicstaticboolOpenDialogMultiple(Filter[]?filters,string?defaultPath,outstring[]?outPaths)
#endif
{
INativeDialogProviderprovider=EnsureProviderAvailable();
returnprovider.OpenDialogMultiple(filters,defaultPath,outoutPaths);
}
#if USE_NOTNULLWHEN
publicstaticboolSaveDialog(Filter[]?filters,string?defaultPath,[NotNullWhen(true)]outstring?outPath)
#else
publicstaticboolSaveDialog(Filter[]?filters,string?defaultPath,outstring?outPath)
#endif
{
INativeDialogProviderprovider=EnsureProviderAvailable();
returnprovider.SaveDialog(filters,defaultPath,outoutPath);
}
#if USE_NOTNULLWHEN
publicstaticboolPickFolder(string?defaultPath,[NotNullWhen(true)]outstring?outPath)
#else
publicstaticboolPickFolder(string?defaultPath,outstring?outPath)
#endif
{
INativeDialogProviderprovider=EnsureProviderAvailable();
returnprovider.PickFolder(defaultPath,outoutPath);
}
staticINativeDialogProviderEnsureProviderAvailable()
{
if(!_providerSearched)
SetDefaultProvider();
if(Providerisnull)
thrownewPlatformNotSupportedException("Dialog provider for current platform is not available!");
returnProvider;
}
publicstructFilter
{
publicstringName;
publicstring[]Extensions;
}
}
}