[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-09
14
Medium Priority
?
1,990 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
0
Comment
Question by:Karl66
  • 8
  • 5
14 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 20044041
       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)
                {

                }
            }
        }
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044049
sorry applicationInterfaceOpenFile is a System.Windows.Forms.OpenFileDialog
0
 

Author Comment

by:Karl66
ID: 20044074
Great!

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

Thanks,

Karl66
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:andrepires
ID: 20044143
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é
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044173
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
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044184
sorry, yes that is for a file but with the suggestion made above and using my code it will work
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044203
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
0
 
LVL 2

Expert Comment

by:andrepires
ID: 20044216
Karl,
If you just need to select a folder, use the FolderBrowserDialog. It was made for that.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044261
andrepires, karl stated that he wants the path.
0
 
LVL 2

Expert Comment

by:andrepires
ID: 20044293
If he want the path, he can use the SelectedPath property of the folderBrowserDialog.
0
 
LVL 2

Accepted Solution

by:
andrepires earned 2000 total points
ID: 20044318
Karl, it would be like this:

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044322
so this :

String direcotryPath = ofdSelectDirectory.FileName;

would need to be:

String direcotryPath = ofdSelectDirectory.SelectedPath;
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20044335
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.
0
 
LVL 2

Assisted Solution

by:andrepires
andrepires earned 2000 total points
ID: 20044389
Ok Karl.
If you use the FolderBrowserDialog to capture the PATH as you stated, your code will be like this:

        private void btnOpen_Click(object sender, EventArgs e)
        {
            String directoryPath = String.Empty;

            // Show the Folder Browser dialog box
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                directoryPath = folderBrowserDialog1.SelectedPath;
            }
        }

Remember to drag and drop the FolderBrowserDialog on your form before using the code above, ok?

Regards,

Andre
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question