943,626 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 20008
  • C# RSS
May 23rd, 2006
0

FolderBrowserDialog problems

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
protos1 is offline Offline
6 posts
since May 2006
May 23rd, 2006
0

Re: FolderBrowserDialog problems

Are you sure?

If I remember correctly th FolderBrowserDialog, shows you all the folders. Even the system root. Or am I mistaken?
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
May 23rd, 2006
0

Re: FolderBrowserDialog problems

C# Syntax (Toggle Plain Text)
  1.  
  2. folderDialog.Description = "Select Folder";
  3. folderDialog.ShowDialog();
  4. if (folderDialog.SelectedPath != string.Empty)
  5. txtTargetPath.Text = folderDialog.SelectedPath;
Reputation Points: 13
Solved Threads: 1
Newbie Poster
MrBenz is offline Offline
4 posts
since May 2006
May 23rd, 2006
1

Re: FolderBrowserDialog problems

Complete Code.

C# Syntax (Toggle Plain Text)
  1. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  2. folderDialog.Description = "Select Folder";
  3. if(folderDialog.ShowDialog() == DialogResult.OK)
  4. {
  5. txtTargetPath.Text = folderDialog.SelectedPath;
  6. }
Team Colleague
Reputation Points: 84
Solved Threads: 99
<Insert title here>
tayspen is offline Offline
1,542 posts
since Jul 2005
May 23rd, 2006
0

Re: FolderBrowserDialog problems

Thanks for your replies.

My code looks like this :

C# Syntax (Toggle Plain Text)
  1. private void BrowseBtn2_Click(object sender, System.EventArgs e)
  2. {
  3. FolderBrowserDialog fbd2 = new FolderBrowserDialog();
  4. fbd2.ShowNewFolderButton = false;
  5. fbd2.RootFolder = Environment.SpecialFolder.MyComputer;
  6.  
  7. DialogResult dr = fbd2.ShowDialog();
  8. if(dr == DialogResult.OK)
  9. {
  10. userDirText2.Text = fbd2.SelectedPath;
  11. this.writeFile("dialog2.txt", userDirText2.Text);
  12. }
  13. }
  14.  
  15.  
  16.  
  17.  
  18. private bool writeFile(string filename, string str)
  19. {
  20. try
  21. {
  22. progressBox.AppendText("Writing // " + filename + " : " + str + "\n");
  23. StreamWriter sw = new StreamWriter(filename);
  24. sw.Write(str);
  25. sw.Flush();
  26. sw.Close();
  27. return true;
  28. }
  29. catch (System.IO.IOException)
  30. {
  31. progressBox.AppendText("IO Error Writing To File.\n");
  32. return false;
  33. }
  34. }


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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
protos1 is offline Offline
6 posts
since May 2006
May 25th, 2006
0

Re: FolderBrowserDialog problems

Your not telling where to write the file, other than jsut a name..try
C# Syntax (Toggle Plain Text)
  1. StreamWriter sw = new StreamWriter(Path.Combine("c:\",filename));
  2. sw.Write(str);
  3. sw.Flush();
  4. sw.Close();
  5.  
Reputation Points: 13
Solved Threads: 1
Newbie Poster
MrBenz is offline Offline
4 posts
since May 2006
May 25th, 2006
0

Re: FolderBrowserDialog problems

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
protos1 is offline Offline
6 posts
since May 2006
May 26th, 2006
0

Re: FolderBrowserDialog problems

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.

C# Syntax (Toggle Plain Text)
  1. private void BrowseBtn2_Click(object sender, System.EventArgs e)
  2. {
  3. FolderBrowserDialog fbd2 = new FolderBrowserDialog();
  4. fbd2.ShowNewFolderButton = false;
  5. fbd2.RootFolder = Environment.SpecialFolder.MyComputer;
  6.  
  7. DialogResult dr = fbd2.ShowDialog();
  8. if(dr == DialogResult.OK)
  9. {
  10. userDirText2.Text = fbd2.SelectedPath;
  11. this.writeFile("dialog2.txt", userDirText2.Text);
  12. }
  13. }
  14.  
  15.  
  16.  
  17.  
  18. private bool writeFile(string filename, string str)
  19. {
  20. bool pass = false;
  21. try
  22. {
  23. progressBox.AppendText("Writing // " + filename + " : " + str + "\n");
  24. StreamWriter sw = new StreamWriter(filename);
  25. sw.Write(str);
  26. sw.Flush();
  27. pass = true;
  28. }
  29. catch (System.IO.IOException)
  30. {
  31. progressBox.AppendText("IO Error Writing To File.\n");
  32. pass = false;
  33. }
  34. finally
  35. {
  36. sw.Close();
  37. }
  38. return pass;
  39. }
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Cannot Test For Condition
Next Thread in C# Forum Timeline: compare 2 xml files with csharp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC