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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun VermaakTechnical SpecialistCommented:
I expect you can just use the file browser dialogue function
System.Windows.Forms.OpenFileDialog
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.