We help IT Professionals succeed at work.

C# Opening a directory to find the path rather than selecting a file ?? ShowDialog() ??

Karl66
Karl66 asked
on
2,013 Views
Last Modified: 2010-08-05
I need to allow a user to browse to and select a directory and then capture that as a string so I can search the directory. I have ...

        private void btnOpen_Click(object sender, EventArgs e)
        {
            // Show the file open dialog box
            if (ofdSelectDirectory.ShowDialog() == DialogResult.OK)
            {
                String direcotryPath = "";
            }
        }

But that does not let me stop at the direrctory level it makes me select a file AND I am not sure how to capture the path even if I did!

Help! Thanks!
Karl66
Comment
Watch Question

CERTIFIED EXPERT

Commented:
       private void btnBrowse_Click(object sender, EventArgs e)
        {
            Stream fileNameStream = null;
            if (applicationInterfaceOpenFile.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    if ((fileNameStream = applicationInterfaceOpenFile.OpenFile()) != null)
                    {

                        txtLocation.Text = applicationInterfaceOpenFile.FileName;

                    }
                }
                catch (Exception)
                {

                }
            }
        }
CERTIFIED EXPERT

Commented:
sorry applicationInterfaceOpenFile is a System.Windows.Forms.OpenFileDialog

Author

Commented:
Great!

Can you please edit your entry with the correctiona dn repost?

Thanks,

Karl66
Hi!
Using the OpenFileDialog will not allow you to select a folder and will force you to select a file.
Try to use the FolderBrowserDialog instead.

Regards,
André
CERTIFIED EXPERT

Commented:
is ofdSelectDirectory an openfiledialog? if so you be able to just plug and play.

you will need to use this to get the directory path

 private void btnOpen_Click(object sender, EventArgs e)
        {
            Stream fileNameStream = null;
            // Show the file open dialog box
            if (ofdSelectDirectory.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((fileNameStream = ofdSelectDirectory.OpenFile()) !=  null)
                    {

                       String direcotryPath = applicationInterfaceOpenFile.FileName;
                   }
                }
                catch (Exception)
                {

                }
            }
        }

 you are probably going to want to declare the direcotryPath as a class variable so you can reuse it
CERTIFIED EXPERT

Commented:
sorry, yes that is for a file but with the suggestion made above and using my code it will work
CERTIFIED EXPERT

Commented:
private void btnOpen_Click(object sender, EventArgs e)
        {
            Stream fileNameStream = null;
            // Show the file open dialog box
            if (ofdSelectDirectory.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((fileNameStream = ofdSelectDirectory.OpenFile()) !=  null)
                    {

                       String direcotryPath = ofdSelectDirectory.FileName;
                   }
                }
                catch (Exception)
                {

                }
            }
        }

one change
Karl,
If you just need to select a folder, use the FolderBrowserDialog. It was made for that.
CERTIFIED EXPERT

Commented:
andrepires, karl stated that he wants the path.
If he want the path, he can use the SelectedPath property of the folderBrowserDialog.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
so this :

String direcotryPath = ofdSelectDirectory.FileName;

would need to be:

String direcotryPath = ofdSelectDirectory.SelectedPath;
CERTIFIED EXPERT

Commented:
dang, got the email right as i posted. but still-- to be more in line with the original code my recent post is what you want.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.