Selecting files directly from a folder using folder browser dialog function

I have used folder browser dialog function to select a folder from a specified directory. But how can I select a file directly from a folder instead of selecting just the folder?
Abhishek ModakStudentAsked:
Who is Participating?
 
MishaProgrammerCommented:
Try this:

private void BrowseButton_Click(object sender, EventArgs e)

{

    OpenFileDialog openFileDialog1 = new OpenFileDialog();

 

    openFileDialog1.InitialDirectory = @"C:\";

    openFileDialog1.Title = "Browse Text Files";

 

    openFileDialog1.CheckFileExists = true;

    openFileDialog1.CheckPathExists = true;

 

    openFileDialog1.DefaultExt = "txt";

    openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

    openFileDialog1.FilterIndex = 2;

    openFileDialog1.RestoreDirectory = true;

 

    openFileDialog1.ReadOnlyChecked = true;

    openFileDialog1.ShowReadOnly = true;

 

    if (openFileDialog1.ShowDialog() == DialogResult.OK)

    {

        MessageBox.Show(openFileDialog1.FileName);

    }

}

Open in new window

0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
I expect you can just use the file browser dialogue function
System.Windows.Forms.OpenFileDialog
0
 
Abhishek ModakStudentAuthor Commented:
I want to select not just text files but also files of other formats as well like audio,video,ppt,pdf etc.
0
 
MishaProgrammerCommented:
If you want to select all files change filter like this:
openFileDialog1.Filter = "All files (*.*)|*.*";

Open in new window


And next row it is not necessary:
openFileDialog1.DefaultExt = "txt";

Open in new window

1
 
Abhishek ModakStudentAuthor Commented:
Thanks for your answers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.