Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2005

Tip: Looking for answers? Try searching our database.

OpenFileDialog - Preventing Browse

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KarenP - 26 Sep 2005 21:56 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.