@@ -178,57 +178,43 @@ public unsafe bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[]
178
178
}
179
179
180
180
/// <inheritdoc/>
181
- public unsafe bool Open_NetworkConnectionDialog ( nint hWind , bool hideRestoreConnectionCheckBox = false , bool persistConnectionAtLogon = false , bool readOnlyPath = false , string ? remoteNetworkName = null , bool useMostRecentPath = false )
181
+ public unsafe bool Open_NetworkConnectionDialog ( nint hWnd , bool hideRestoreConnectionCheckBox = false , bool persistConnectionAtLogon = false , bool readOnlyPath = false , string ? remoteNetworkName = null , bool useMostRecentPath = false )
182
182
{
183
- NETRESOURCEW netRes = default ;
184
- CONNECTDLGSTRUCTW dialogOptions = default ;
183
+ if ( useMostRecentPath && ! string . IsNullOrEmpty ( remoteNetworkName ) )
184
+ throw new ArgumentException ( $ " { nameof ( useMostRecentPath ) } cannot be set to true if { nameof ( remoteNetworkName ) } has a value." ) ;
185
185
186
- dialogOptions . cbStructure = ( uint ) Marshal . SizeOf ( typeof ( CONNECTDLGSTRUCTW ) ) ;
187
- netRes . dwType = NET_RESOURCE_TYPE . RESOURCETYPE_DISK ;
186
+ NETRESOURCEW netResource = default ;
187
+ CONNECTDLGSTRUCTW connectDlgOptions = default ;
188
+ WIN32_ERROR res = default ;
188
189
189
190
if ( hideRestoreConnectionCheckBox )
190
- dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_HIDE_BOX ;
191
- else
192
- dialogOptions . dwFlags &= ~ CONNECTDLGSTRUCT_FLAGS . CONNDLG_HIDE_BOX ;
193
-
191
+ connectDlgOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_HIDE_BOX ;
194
192
if ( persistConnectionAtLogon )
195
- {
196
- dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_PERSIST ;
197
- dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_NOT_PERSIST ;
198
- }
199
- else
200
- {
201
- dialogOptions . dwFlags &= ~ CONNECTDLGSTRUCT_FLAGS . CONNDLG_PERSIST ;
202
- dialogOptions . dwFlags &= ~ CONNECTDLGSTRUCT_FLAGS . CONNDLG_NOT_PERSIST ;
203
- }
204
-
205
- fixed ( char * lpcRemoteName = remoteNetworkName )
206
- netRes . lpRemoteName = lpcRemoteName ;
207
-
208
- if ( useMostRecentPath && ! string . IsNullOrEmpty ( remoteNetworkName ) )
209
- throw new InvalidOperationException ( $ "{ nameof ( useMostRecentPath ) } cannot be set to true if { nameof ( remoteNetworkName ) } has a value.") ;
210
-
193
+ connectDlgOptions . dwFlags |= ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_PERSIST & CONNECTDLGSTRUCT_FLAGS . CONNDLG_NOT_PERSIST ) ;
211
194
if ( useMostRecentPath )
212
- dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_USE_MRU ;
213
- else
214
- dialogOptions . dwFlags &= ~ CONNECTDLGSTRUCT_FLAGS . CONNDLG_USE_MRU ;
215
-
216
- dialogOptions . hwndOwner = new ( hWind ) ;
195
+ connectDlgOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_USE_MRU ;
196
+ if ( readOnlyPath && ! string . IsNullOrEmpty ( remoteNetworkName ) )
197
+ connectDlgOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_RO_PATH ;
217
198
218
- dialogOptions . lpConnRes = & netRes ;
219
-
220
- if ( readOnlyPath && ! string . IsNullOrEmpty ( netRes . lpRemoteName . ToString ( ) ) )
221
- dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_RO_PATH ;
199
+ fixed ( char * pszRemoteName = remoteNetworkName )
200
+ {
201
+ netResource . dwType = NET_RESOURCE_TYPE . RESOURCETYPE_DISK ;
202
+ netResource . lpRemoteName = pszRemoteName ;
222
203
223
- var result = PInvoke . WNetConnectionDialog1W ( ref dialogOptions ) ;
204
+ connectDlgOptions . cbStructure = ( uint ) sizeof ( CONNECTDLGSTRUCTW ) ;
205
+ connectDlgOptions . hwndOwner = new ( hWnd ) ;
206
+ connectDlgOptions . lpConnRes = & netResource ;
224
207
225
- dialogOptions . lpConnRes = null ;
208
+ res = PInvoke . WNetConnectionDialog1W ( ref connectDlgOptions ) ;
209
+ }
226
210
227
- if ( ( uint ) result == unchecked ( ( uint ) - 1 ) )
211
+ // User canceled
212
+ if ( ( uint ) res == unchecked ( ( uint ) - 1 ) )
228
213
return false ;
229
214
230
- if ( result == 0 )
231
- throw new Win32Exception ( "Cannot display dialog" ) ;
215
+ // Unexpected error happened
216
+ if ( res is not WIN32_ERROR . NO_ERROR )
217
+ throw new Win32Exception ( "Failed to process the network connection dialog successfully." ) ;
232
218
233
219
return true ;
234
220
}
0 commit comments