BrowseForFolder.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* ***************************************
  2. * BrowseForFolder.cs
  3. * ---------------------------------------
  4. * - Requirements -
  5. *
  6. * You have to include the
  7. * System.Design.dll in your References
  8. * (Project/Add Reference from menu)
  9. * ---------------------------------------
  10. * - Example -
  11. *
  12. string myPath;
  13. BrowseForFolderClass myFolderBrowser = new BrowseForFolderClass();
  14. myPath = myFolderBrowser.BrowseForFolder("Please, select a folder");
  15. if (myPath.Length > 0)
  16. txtPath.Text = myPath;
  17. * ****************************************/
  18. using System;
  19. using System.Windows.Forms;
  20. using System.Windows.Forms.Design;
  21. namespace Utility.BrowseForFolder
  22. {
  23. public class BrowseForFolderClass : FolderNameEditor
  24. {
  25. FolderNameEditor.FolderBrowser myFolderBrowser;
  26. public string BrowseForFolder(string title)
  27. {
  28. string myPath;
  29. myFolderBrowser = new FolderNameEditor.FolderBrowser();
  30. // Description
  31. myFolderBrowser.Description = title;
  32. // ShowDialog
  33. myFolderBrowser.ShowDialog();
  34. // DirectoryPath
  35. myPath = myFolderBrowser.DirectoryPath;
  36. // Shall I add the "\" character at the end of the path ?
  37. if (myPath.Length > 0)
  38. {
  39. if (myPath.Substring((myPath.Length - 1),1) != "\\")
  40. myPath += "\\";
  41. }
  42. // Return correct path
  43. return myPath;
  44. }
  45. ~BrowseForFolderClass()
  46. {
  47. myFolderBrowser.Dispose(); // Dispose
  48. }
  49. }
  50. }