Hi,
I would like to use the OpenFileDialog to allow the user to select a file
for processing. Is there any way I can prevent the user from browsing to
folders other than the InitialDirectory? For security reasons, I cannot
allow users to browse to other folders on the computer.
Thanks,
Karen Prengaman
Oliver Sturm - 27 Sep 2005 10:16 GMT
>I would like to use the OpenFileDialog to allow the user to select a file
>for processing. Is there any way I can prevent the user from browsing to
>folders other than the InitialDirectory? For security reasons, I cannot
>allow users to browse to other folders on the computer.
I don't think there's an easy way to do this. I guess it might be possible
somehow by using the HookProc of the common dialog to modify the behaviour
of the open file dialog.
If this was my project, I would probably just not use the open file
dialog. It would be easy to create your own control based on a ListView,
that would show the files and directories in the current folder - and it
would probably be safer, considering you want to do this for security
reasons.
Oliver Sturm

Signature
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Herfried K. Wagner [MVP] - 27 Sep 2005 12:21 GMT
"KarenP" <KarenP@discussions.microsoft.com> schrieb:
> I would like to use the OpenFileDialog to allow the user to select a file
> for processing. Is there any way I can prevent the user from browsing to
> folders other than the InitialDirectory? For security reasons, I cannot
> allow users to browse to other folders on the computer.
As Oliver says, you'll need a dialog hook to archieve this behavior.
However, similar, but far less user-friendly behavior can be archieved as
shown below:
\\\
Private Sub OpenFileDialog1_FileOk( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles OpenFileDialog1.FileOk
If _
Me.OpenFileDialog1.FileName <> _
Path.Combine( _
"C:\WINDOWS", _
Path.GetFileName(Me.OpenFileDialog1.FileName) _
) _
Then
MsgBox( _
"You must select a file in ""C:\WINDOWS""!", _
MsgBoxStyle.Exclamation _
)
e.Cancel = True
End If
End Sub
Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
Me.OpenFileDialog1.InitialDirectory = "C:\WINDOWS"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
MsgBox("Selected file: """ & Me.OpenFileDialog1.FileName & """.")
End If
End Sub
///

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
KarenP - 28 Sep 2005 00:26 GMT
Thanks both of you for your responses. I think I will just create my own
dialog to handle this. I was hoping there was some not-well-documented code
or property I could use on the OpenFileDialog, but I guess not.
Thanks again,
Karen