Hi
Has anyone dealt with this windows control ?

I'm trying to get it to work so that the operator can select ANY folder, but it looks like you have to set a RootFolder property to one of several
enumerated options, and if the folder that the user selects is not a subfolder of the chosen RootFolder, then tough luck.
And of course none of the enumerated options for the rootfolder property includes the filesystem root ....................

I've been fighting with it for three days and getting nowhere - has anyone else dealt with this ?


Thanks,
Colm

Recommended Answers

All 7 Replies

Are you sure?

If I remember correctly th FolderBrowserDialog, shows you all the folders. Even the system root. Or am I mistaken?

folderDialog.Description = "Select Folder";
            folderDialog.ShowDialog();
            if (folderDialog.SelectedPath != string.Empty)
                txtTargetPath.Text = folderDialog.SelectedPath;

Complete Code.

FolderBrowserDialog folderDialog = new FolderBrowserDialog();
folderDialog.Description = "Select Folder";
if(folderDialog.ShowDialog() == DialogResult.OK)
{
txtTargetPath.Text = folderDialog.SelectedPath; 
}

Thanks for your replies.

My code looks like this :

private void BrowseBtn2_Click(object sender, System.EventArgs e)
        {
            FolderBrowserDialog fbd2 = new FolderBrowserDialog();
            fbd2.ShowNewFolderButton = false;
            fbd2.RootFolder = Environment.SpecialFolder.MyComputer;

            DialogResult dr = fbd2.ShowDialog();
            if(dr == DialogResult.OK)        
            {
                userDirText2.Text = fbd2.SelectedPath;
                this.writeFile("dialog2.txt", userDirText2.Text);
            }
        }




        private bool writeFile(string filename, string str)
        {
            try
            {        
                progressBox.AppendText("Writing // " + filename + " : " + str + "\n");
                StreamWriter sw = new StreamWriter(filename);
                sw.Write(str);
                sw.Flush();
                sw.Close();
                return true;
            }
            catch (System.IO.IOException) 
            {
                progressBox.AppendText("IO Error Writing To File.\n");
                return false;
            }
        }

This brings up a dialog box with its root folder set to "my computer"
If I just print the selectedPath to a messageBox it looks fine, but when I try and write it to file it doesn't work.

If I leave the rootFolder at the default (the desktop) and select a folder which exists on the desktop (is a desktop subfolder) then it works fine.

Any ideas ?

Thanks
Colm

Your not telling where to write the file, other than jsut a name..try

StreamWriter sw = new StreamWriter(Path.Combine("c:\",filename));
                sw.Write(str);
                sw.Flush();
                sw.Close();

I fixed it yesterday. You're right - I assumed that with no path it would write the file to the same directory as where the exe is located, and usually it does, but selecting a different folder whth the folder dialog changes the default .................
Setting the path worked.

Thanks
Colm

If there is an error while writting in your code the file will not close,make sure you close that file if there is an error. otherwise if it stays open youll just get more errors trying to open an already open file.
this is how i typically would do it.

private void BrowseBtn2_Click(object sender, System.EventArgs e)
        {
            FolderBrowserDialog fbd2 = new FolderBrowserDialog();
            fbd2.ShowNewFolderButton = false;
            fbd2.RootFolder = Environment.SpecialFolder.MyComputer;

            DialogResult dr = fbd2.ShowDialog();
            if(dr == DialogResult.OK)        
            {
                userDirText2.Text = fbd2.SelectedPath;
                this.writeFile("dialog2.txt", userDirText2.Text);
            }
        }




        private bool writeFile(string filename, string str)
        {
            bool pass = false;
            try
            {        
                progressBox.AppendText("Writing // " + filename + " : " + str + "\n");
                StreamWriter sw = new StreamWriter(filename);
                sw.Write(str);
                sw.Flush();
                pass =  true;
            }
            catch (System.IO.IOException) 
            {
                progressBox.AppendText("IO Error Writing To File.\n");
                pass = false;
            }
            finally
            {
            sw.Close();
            }
return pass;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.