FolderBrowserDialog problems

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 6
Reputation: protos1 is an unknown quantity at this point 
Solved Threads: 0
protos1 protos1 is offline Offline
Newbie Poster

FolderBrowserDialog problems

 
0
  #1
May 23rd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: FolderBrowserDialog problems

 
0
  #2
May 23rd, 2006
Are you sure?

If I remember correctly th FolderBrowserDialog, shows you all the folders. Even the system root. Or am I mistaken?
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: MrBenz is an unknown quantity at this point 
Solved Threads: 1
MrBenz MrBenz is offline Offline
Newbie Poster

Re: FolderBrowserDialog problems

 
0
  #3
May 23rd, 2006
  1.  
  2. folderDialog.Description = "Select Folder";
  3. folderDialog.ShowDialog();
  4. if (folderDialog.SelectedPath != string.Empty)
  5. txtTargetPath.Text = folderDialog.SelectedPath;
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,542
Reputation: tayspen is on a distinguished road 
Solved Threads: 98
Team Colleague
tayspen's Avatar
tayspen tayspen is offline Offline
<Insert title here>

Re: FolderBrowserDialog problems

 
1
  #4
May 23rd, 2006
Complete Code.

  1. FolderBrowserDialog folderDialog = new FolderBrowserDialog();
  2. folderDialog.Description = "Select Folder";
  3. if(folderDialog.ShowDialog() == DialogResult.OK)
  4. {
  5. txtTargetPath.Text = folderDialog.SelectedPath;
  6. }
Firefox
Ewido
Tune up windows
Get detailed system information
My Fixes

Member - Alliance of Security Analysis Professionals - Since 2006
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: protos1 is an unknown quantity at this point 
Solved Threads: 0
protos1 protos1 is offline Offline
Newbie Poster

Re: FolderBrowserDialog problems

 
0
  #5
May 23rd, 2006
Thanks for your replies.

My code looks like this :

  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4
Reputation: MrBenz is an unknown quantity at this point 
Solved Threads: 1
MrBenz MrBenz is offline Offline
Newbie Poster

Re: FolderBrowserDialog problems

 
0
  #6
May 25th, 2006
Your not telling where to write the file, other than jsut a name..try
  1. StreamWriter sw = new StreamWriter(Path.Combine("c:\",filename));
  2. sw.Write(str);
  3. sw.Flush();
  4. sw.Close();
  5.  
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 6
Reputation: protos1 is an unknown quantity at this point 
Solved Threads: 0
protos1 protos1 is offline Offline
Newbie Poster

Re: FolderBrowserDialog problems

 
0
  #7
May 25th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: FolderBrowserDialog problems

 
0
  #8
May 26th, 2006
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.

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC