Amit Kumar
Amit Kumar

Reputation: 1059

Open only local drives in open file dialog

I want to open a "OpenFileDialog", on which we can select only local hard drives (ex. C:\,D:). I want to add restrictions to access network drives. I have used following code but I am able to access the network drive.

Code is given below:

OpenFileDialog dialog = new OpenFileDialog();
            dialog.CustomPlaces.Clear();
            foreach (DriveInfo Drive in DriveInfo.GetDrives())
            {
                if (Drive.DriveType == DriveType.Fixed)
                {
                    dialog.CustomPlaces.Add(Drive.Name);
                }
            }
            dialog.ShowDialog();

Upvotes: 1

Views: 1766

Answers (1)

Tim
Tim

Reputation: 15237

I don't think this is possible with the built-in OpenfileDialog. Changing the CustomPlaces is just the list of "custom" places you want pinned on the top left. It doesn't limit the places they can go.

I think you'll have to either write a custom dialog (ick!) or do something to validate their selection after they've hit ok.

Upvotes: 3

Related Questions