77
88namespace SOTFEdit . Infrastructure ;
99
10- // for WPF support
11-
12- // for WPF support
13-
1410//Source: https://stackoverflow.com/questions/11624298/how-do-i-use-openfiledialog-to-select-a-folder
1511public class FolderPicker
1612{
17- #pragma warning disable IDE1006 // Naming Styles
18- private const int ERROR_CANCELLED = unchecked ( ( int ) 0x800704C7 ) ;
19- #pragma warning restore IDE1006 // Naming Styles
20- public virtual string ResultPath { get ; protected set ; }
21- public virtual string ResultName { get ; protected set ; }
22- public virtual string InputPath { get ; set ; }
13+ private const int ErrorCancelled = unchecked ( ( int ) 0x800704C7 ) ;
14+ public virtual string ? ResultPath { get ; protected set ; }
15+ public virtual string ? ResultName { get ; protected set ; }
16+ public virtual string ? InputPath { get ; set ; }
2317 public virtual bool ForceFileSystem { get ; set ; }
24- public virtual string Title { get ; set ; }
25- public virtual string OkButtonLabel { get ; set ; }
26- public virtual string FileNameLabel { get ; set ; }
18+ public virtual string ? Title { get ; set ; }
19+ public virtual string ? OkButtonLabel { get ; set ; }
20+ public virtual string ? FileNameLabel { get ; set ; }
2721
2822 protected virtual int SetOptions ( int options )
2923 {
3024 if ( ForceFileSystem )
3125 {
32- options |= ( int ) FOS . FOS_FORCEFILESYSTEM ;
26+ options |= ( int ) Fos . FosForcefilesystem ;
3327 }
3428
3529 return options ;
3630 }
3731
3832 // for WPF support
39- public bool ? ShowDialog ( Window owner = null , bool throwOnError = false )
33+ public bool ? ShowDialog ( Window ? owner = null , bool throwOnError = false )
4034 {
4135 owner ??= Application . Current . MainWindow ;
4236 return ShowDialog ( owner != null ? new WindowInteropHelper ( owner ) . Handle : IntPtr . Zero , throwOnError ) ;
@@ -45,6 +39,7 @@ protected virtual int SetOptions(int options)
4539 // for all .NET
4640 public virtual bool ? ShowDialog ( IntPtr owner , bool throwOnError = false )
4741 {
42+ // ReSharper disable once SuspiciousTypeConversion.Global
4843 var dialog = ( IFileOpenDialog ) new FileOpenDialog ( ) ;
4944 if ( ! string . IsNullOrEmpty ( InputPath ) )
5045 {
@@ -57,8 +52,8 @@ protected virtual int SetOptions(int options)
5752 dialog . SetFolder ( item ) ;
5853 }
5954
60- var options = FOS . FOS_PICKFOLDERS ;
61- options = ( FOS ) SetOptions ( ( int ) options ) ;
55+ var options = Fos . FosPickfolders ;
56+ options = ( Fos ) SetOptions ( ( int ) options ) ;
6257 dialog . SetOptions ( options ) ;
6358
6459 if ( Title != null )
@@ -86,7 +81,7 @@ protected virtual int SetOptions(int options)
8681 }
8782
8883 var hr = dialog . Show ( owner ) ;
89- if ( hr == ERROR_CANCELLED )
84+ if ( hr == ErrorCancelled )
9085 {
9186 return null ;
9287 }
@@ -101,14 +96,14 @@ protected virtual int SetOptions(int options)
10196 return null ;
10297 }
10398
104- if ( CheckHr ( result . GetDisplayName ( SIGDN . SIGDN_DESKTOPABSOLUTEPARSING , out var path ) , throwOnError ) != 0 )
99+ if ( CheckHr ( result . GetDisplayName ( Sigdn . SigdnDesktopabsoluteparsing , out var path ) , throwOnError ) != 0 )
105100 {
106101 return null ;
107102 }
108103
109104 ResultPath = path ;
110105
111- if ( CheckHr ( result . GetDisplayName ( SIGDN . SIGDN_DESKTOPABSOLUTEEDITING , out path ) , false ) == 0 )
106+ if ( CheckHr ( result . GetDisplayName ( Sigdn . SigdnDesktopabsoluteediting , out path ) , false ) == 0 )
112107 {
113108 ResultName = path ;
114109 }
@@ -130,8 +125,8 @@ private static int CheckHr(int hr, bool throwOnError)
130125 }
131126
132127 [ DllImport ( "shell32" ) ]
133- private static extern int SHCreateItemFromParsingName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszPath ,
134- IBindCtx pbc , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid riid , out IShellItem ppv ) ;
128+ private static extern int SHCreateItemFromParsingName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string ? pszPath ,
129+ IBindCtx ? pbc , [ MarshalAs ( UnmanagedType . LPStruct ) ] Guid riid , out IShellItem ppv ) ;
135130
136131 [ DllImport ( "user32" ) ]
137132 private static extern IntPtr GetDesktopWindow ( ) ;
@@ -166,10 +161,10 @@ private interface IFileOpenDialog
166161 int Unadvise ( ) ;
167162
168163 [ PreserveSig ]
169- int SetOptions ( FOS fos ) ;
164+ int SetOptions ( Fos fos ) ;
170165
171166 [ PreserveSig ]
172- int GetOptions ( out FOS pfos ) ;
167+ int GetOptions ( out Fos pfos ) ;
173168
174169 [ PreserveSig ]
175170 int SetDefaultFolder ( IShellItem psi ) ;
@@ -184,16 +179,16 @@ private interface IFileOpenDialog
184179 int GetCurrentSelection ( out IShellItem ppsi ) ;
185180
186181 [ PreserveSig ]
187- int SetFileName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszName ) ;
182+ int SetFileName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string ? pszName ) ;
188183
189184 [ PreserveSig ]
190185 int GetFileName ( [ MarshalAs ( UnmanagedType . LPWStr ) ] out string pszName ) ;
191186
192187 [ PreserveSig ]
193- int SetTitle ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszTitle ) ;
188+ int SetTitle ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string ? pszTitle ) ;
194189
195190 [ PreserveSig ]
196- int SetOkButtonLabel ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszText ) ;
191+ int SetOkButtonLabel ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string ? pszText ) ;
197192
198193 [ PreserveSig ]
199194 int SetFileNameLabel ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszLabel ) ;
@@ -238,7 +233,7 @@ private interface IShellItem
238233 int GetParent ( ) ; // not fully defined
239234
240235 [ PreserveSig ]
241- int GetDisplayName ( SIGDN sigdnName , [ MarshalAs ( UnmanagedType . LPWStr ) ] out string ppszName ) ;
236+ int GetDisplayName ( Sigdn sigdnName , [ MarshalAs ( UnmanagedType . LPWStr ) ] out string ? ppszName ) ;
242237
243238 [ PreserveSig ]
244239 int GetAttributes ( ) ; // not fully defined
@@ -248,45 +243,49 @@ private interface IShellItem
248243 }
249244
250245#pragma warning disable CA1712 // Do not prefix enum values with type name
251- private enum SIGDN : uint
246+ private enum Sigdn : uint
252247 {
253- SIGDN_DESKTOPABSOLUTEEDITING = 0x8004c000 ,
254- SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000 ,
255- SIGDN_FILESYSPATH = 0x80058000 ,
256- SIGDN_NORMALDISPLAY = 0 ,
257- SIGDN_PARENTRELATIVE = 0x80080001 ,
258- SIGDN_PARENTRELATIVEEDITING = 0x80031001 ,
259- SIGDN_PARENTRELATIVEFORADDRESSBAR = 0x8007c001 ,
260- SIGDN_PARENTRELATIVEPARSING = 0x80018001 ,
261- SIGDN_URL = 0x80068000
248+ SigdnDesktopabsoluteediting = 0x8004c000 ,
249+
250+ SigdnDesktopabsoluteparsing = 0x80028000
251+ /*
252+ SigdnFilesyspath = 0x80058000,
253+ SigdnNormaldisplay = 0,
254+ SigdnParentrelative = 0x80080001,
255+ SigdnParentrelativeediting = 0x80031001,
256+ SigdnParentrelativeforaddressbar = 0x8007c001,
257+ SigdnParentrelativeparsing = 0x80018001,
258+ SigdnUrl = 0x80068000*/
262259 }
263260
264261 [ Flags ]
265- private enum FOS
262+ private enum Fos
266263 {
267- FOS_OVERWRITEPROMPT = 0x2 ,
268- FOS_STRICTFILETYPES = 0x4 ,
269- FOS_NOCHANGEDIR = 0x8 ,
270- FOS_PICKFOLDERS = 0x20 ,
271- FOS_FORCEFILESYSTEM = 0x40 ,
272- FOS_ALLNONSTORAGEITEMS = 0x80 ,
273- FOS_NOVALIDATE = 0x100 ,
274- FOS_ALLOWMULTISELECT = 0x200 ,
275- FOS_PATHMUSTEXIST = 0x800 ,
276- FOS_FILEMUSTEXIST = 0x1000 ,
277- FOS_CREATEPROMPT = 0x2000 ,
278- FOS_SHAREAWARE = 0x4000 ,
279- FOS_NOREADONLYRETURN = 0x8000 ,
280- FOS_NOTESTFILECREATE = 0x10000 ,
281- FOS_HIDEMRUPLACES = 0x20000 ,
282- FOS_HIDEPINNEDPLACES = 0x40000 ,
283- FOS_NODEREFERENCELINKS = 0x100000 ,
284- FOS_OKBUTTONNEEDSINTERACTION = 0x200000 ,
285- FOS_DONTADDTORECENT = 0x2000000 ,
286- FOS_FORCESHOWHIDDEN = 0x10000000 ,
287- FOS_DEFAULTNOMINIMODE = 0x20000000 ,
288- FOS_FORCEPREVIEWPANEON = 0x40000000 ,
289- FOS_SUPPORTSTREAMABLEITEMS = unchecked ( ( int ) 0x80000000 )
264+ /*
265+ FosOverwriteprompt = 0x2,
266+ FosStrictfiletypes = 0x4,
267+ FosNochangedir = 0x8,*/
268+ FosPickfolders = 0x20 ,
269+
270+ FosForcefilesystem = 0x40
271+ /*FosAllnonstorageitems = 0x80,
272+ FosNovalidate = 0x100,
273+ FosAllowmultiselect = 0x200,
274+ FosPathmustexist = 0x800,
275+ FosFilemustexist = 0x1000,
276+ FosCreateprompt = 0x2000,
277+ FosShareaware = 0x4000,
278+ FosNoreadonlyreturn = 0x8000,
279+ FosNotestfilecreate = 0x10000,
280+ FosHidemruplaces = 0x20000,
281+ FosHidepinnedplaces = 0x40000,
282+ FosNodereferencelinks = 0x100000,
283+ FosOkbuttonneedsinteraction = 0x200000,
284+ FosDontaddtorecent = 0x2000000,
285+ FosForceshowhidden = 0x10000000,
286+ FosDefaultnominimode = 0x20000000,
287+ FosForcepreviewpaneon = 0x40000000,
288+ FosSupportstreamableitems = unchecked((int)0x80000000)*/
290289 }
291290#pragma warning restore CA1712 // Do not prefix enum values with type name
292291}
0 commit comments