0

With folderBrowseDialog you can show the top level of the folder which will be shown initially. However that feature is not there on openFileDialog.

I want to let the user open files from a specific folder only. How do I specify the folder?

Gawjus Gurl
  • 97
  • 1
  • 5

3 Answers3

1

Set InitialDirectory property:

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = @"D:\SomeFolder";
dialog.ShowDialog();
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks! But incase the files are in a different folder. Need to point to the application's folder no matter where it is – Gawjus Gurl Jan 30 '14 at 16:19
  • I have a folder named object_files in my project folder and i need the file opened to be copied there. So in order to mention the destination folder. rather than typing the long path i want to find a way to specify the path. And the path may change if the project folder is moved elsewhere. – Gawjus Gurl Jan 30 '14 at 16:37
1

Just set the openFileDialog.InitialDirectory

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = @"C:\";
openFileDialog.ShowDialog();
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

Gawjus, answering your question "How can I restrict the user from accessing other folders? can I lock them?", you can use Environment Special Folder as per code below, but as far I know you cannot define a custom folder, there are some options available. Another way it's to create an UserControl that list only files from a specific folder. Doing that you can have more control how it will behave.

ofd.RootFolder = Environment.SpecialFolder.MyDocuments;