Praveen Ez
Praveen Ez

Reputation: 1

opening a file browser which has no access to the local drive C in C#

var  dlgs = new System.Windows.Forms.OpenFileDialog();
       dlgs.CustomPlaces.Clear();
       var ListDrives = DriveInfo.GetDrives();
       foreach (DriveInfo Drive in ListDrives)
       {
            if ((Drive.DriveType == DriveType.Fixed) && (Drive.Name != "C"))
            {
                dlgs.CustomPlaces.Add(Drive.Name);

            }
           dlgs.ShowDialog();
       }

I am trying to open a file browser that should not have access to the local drive C so the user can select folders are files from rest of the local Drives like ("D" , "E").

Upvotes: 0

Views: 753

Answers (2)

Jcl
Jcl

Reputation: 28272

It's not possible to restrict where the user can access in the dialog itself (unless you implement your own dialog).

It's however possible to restrict whether the file can be opened (whether pressing the Open button or double-clicking will actually close the dialog), using the FileOk event.

Something like:

void DialogFileOk(object sender, CancelEventArgs e)
{
  var dialog = sender as OpenFileDialog;
  if(dialog == null) return;
  if(Path.GetPathRoot(dialog.FileName) == @"C:\")
  {
    e.Cancel = true;
    // maybe show messagebox or task dialog informing the user?
  }
}

Again, this won't prevent users browsing the C:\ drive, it only prevents the dialog from selecting a file in that drive.

PS: adapt for multi-selection if needed.

Upvotes: 1

Wanda B.
Wanda B.

Reputation: 171

I'm just looking at the OpenFileDialogue class documentation now, but I don't see anything that would restrict the user to certain drives... This post makes me curious as to whether or not it could actually be done; but perhaps it could be accomplished using a filter...

Upvotes: 1

Related Questions