0
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").

Taterhead
  • 5,763
  • 4
  • 31
  • 40
  • 2
    Clarify. What is your problem? What is the effect of your current code? Also, You're calling `ShowDialog()` _inside_ your foreach. – Nyerguds Mar 25 '16 at 09:57
  • Possible duplicate of [C# OpenFileDialog Lock To Directory](http://stackoverflow.com/questions/1635846/c-sharp-openfiledialog-lock-to-directory) – MUG4N Mar 25 '16 at 10:02
  • Your best bet would be to create a custom popup which could be custom control or user control in WPF. I had done it in the past. Fairly simple it will be and you can do anything you want. – Nikhil Vartak Mar 25 '16 at 10:46

2 Answers2

1

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...

Community
  • 1
  • 1
Wanda B.
  • 161
  • 2
  • 11
  • But Filter is for files right !! Can it be used to restrict the Drives access in the file browser..?? – Praveen Ez Mar 25 '16 at 10:24
  • @PraveenEz Ah shoot, I think you're right... So you basically want the file browser to ignore the existence of C: and only allow users to select files from other drives (e.g. D: and E:)? I was thinking that there would be some way to specify "all files not starting with C: in absolute path"... I have read now a bit more, and it seems like you'll have to [create your own](https://social.msdn.microsoft.com/Forums/vstudio/en-US/ae322891-fd55-4027-ae4f-53100ae32852/openfiledialog-restrict-directory-browsing?forum=csharpgeneral) custom file browser pop up! – Wanda B. Mar 25 '16 at 18:11
1

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.

Jcl
  • 27,696
  • 5
  • 61
  • 92