Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for openfolderbox.cs

(view source code of openfolderbox.cs as plain text)

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class OpenFolderBox
  9. 	{
  10. 		static string progver = "1.01";
  11.  
  12. 		[STAThread]
  13. 		static int Main( string[] args )
  14. 		{
  15. 			try
  16. 			{
  17. 				using ( FolderBrowserDialog dialog = new FolderBrowserDialog( ) )
  18. 				{
  19. 					string startfolder = Directory.GetCurrentDirectory( );
  20. 					string description = "OpenFolderBox,  Version " + progver;
  21. 					bool startfolderset = false;
  22. 					bool descriptionset = false;
  23. 					bool allowmakedir = false;
  24.  
  25. 					if ( args.Length > 3 )
  26. 					{
  27. 						return WriteError( "Too many command line arguments" );
  28. 					}
  29.  
  30. 					if ( args.Length > 0 )
  31. 					{
  32. 						foreach ( string arg in args )
  33. 						{
  34. 							switch ( arg.ToUpper( ) )
  35. 							{
  36. 								case "/?":
  37. 									return WriteError( );
  38. 								case "/MD":
  39. 									allowmakedir = true;
  40. 									break;
  41. 								default:
  42. 									if ( startfolderset )
  43. 									{
  44. 										if ( descriptionset )
  45. 										{
  46. 											return WriteError( String.Format( "Invalid or duplicate command line argument \"{0}\"", arg ) );
  47. 										}
  48. 										description = arg.Replace( "\\n", "\n" ).Replace( "\\t", "\t" );
  49. 										descriptionset = true;
  50. 									}
  51. 									else
  52. 									{
  53. 										startfolder = arg;
  54. 										startfolderset = true;
  55. 										if ( !Directory.Exists( startfolder ) )
  56. 										{
  57. 											return WriteError( String.Format( "Invalid folder \"{0}\"", startfolder ) );
  58. 										}
  59. 									}
  60. 									break;
  61. 							}
  62. 						}
  63. 					}
  64. 					dialog.SelectedPath = startfolder;
  65. 					dialog.Description = description;
  66. 					dialog.ShowNewFolderButton = allowmakedir;
  67. 					if ( dialog.ShowDialog( ) == DialogResult.OK )
  68. 					{
  69. 						Console.WriteLine( dialog.SelectedPath );
  70. 						return 0;
  71. 					}
  72. 					else
  73. 					{
  74. 						// Cancel was clicked
  75. 						return 2;
  76. 					}
  77. 				}
  78. 			}
  79. 			catch ( Exception e )
  80. 			{
  81. 				return WriteError( e.Message );
  82. 			}
  83. 		}
  84.  
  85. 		public static int WriteError( string errorMessage = null )
  86. 		{
  87. 			/*
  88. 			OpenFolderBox.exe,  Version 1.01
  89. 			Batch tool to present a Browse Folders Dialog and return the selected path
  90.  
  91. 			Usage:  OPENFOLDERBOX  [ "startfolder"  [ "description" ] ]  [ /MD ]
  92.  
  93. 			Where:  "startfolder"  is the initial folder the dialog will show on opening
  94. 			                       (default: current directory)
  95. 			        "description"  is the text above the dialog's tree view
  96. 			                       (default: program name and version)
  97. 			        /MD            display the "Make New Folder" button
  98. 			                       (default: hide the button)
  99.  
  100. 			Notes:  Though the "Make New Folder" button is hidden by default, this does
  101. 			        not inhibit manipulating folders using right-click or Shift+F10.
  102. 			        The full path of the selected folder is written to Standard Output
  103. 			        if OK was clicked, or an empty string if Cancel was clicked.
  104. 			        The return code will be 0 on success, 1 in case of (command line)
  105. 			        errors, or 2 if Cancel was clicked.
  106.  
  107. 			Written by Rob van der Woude
  108. 			http://www.robvanderwoude.com
  109. 			*/
  110.  
  111. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  112. 			{
  113. 				Console.Error.WriteLine( );
  114. 				Console.ForegroundColor = ConsoleColor.Red;
  115. 				Console.Error.Write( "ERROR: " );
  116. 				Console.ForegroundColor = ConsoleColor.White;
  117. 				Console.Error.WriteLine( errorMessage );
  118. 				Console.ResetColor( );
  119. 			}
  120. 			Console.Error.WriteLine( );
  121. 			Console.Error.WriteLine( "OpenFolderBox.exe,  Version {0}", progver );
  122. 			Console.Error.WriteLine( "Batch tool to present a Browse Folders Dialog and return the selected path" );
  123. 			Console.Error.WriteLine( );
  124.  
  125. 			Console.Error.Write( "Usage:  " );
  126. 			Console.ForegroundColor = ConsoleColor.White;
  127. 			Console.Error.WriteLine( "OPENFOLDERBOX  [ \"startfolder\"  [ \"description\" ] ]  [ /MD ]" );
  128. 			Console.ResetColor( );
  129.  
  130. 			Console.Error.WriteLine( );
  131.  
  132. 			Console.Error.Write( "Where:  " );
  133. 			Console.ForegroundColor = ConsoleColor.White;
  134. 			Console.Error.Write( "\"startfolder\"" );
  135. 			Console.ResetColor( );
  136. 			Console.Error.WriteLine( "  is the initial folder the dialog will show on opening" );
  137.  
  138. 			Console.Error.WriteLine( "                       (default: current directory)" );
  139.  
  140. 			Console.ForegroundColor = ConsoleColor.White;
  141. 			Console.Error.Write( "        \"description\"" );
  142. 			Console.ResetColor( );
  143. 			Console.Error.WriteLine( "  is the text above the dialog's tree view" );
  144.  
  145. 			Console.Error.WriteLine( "                       (default: \"OpenFolderBox,  Version {0}\")", progver );
  146.  
  147. 			Console.ForegroundColor = ConsoleColor.White;
  148. 			Console.Error.Write( "        /MD" );
  149. 			Console.ResetColor( );
  150. 			Console.Error.Write( "            display the \"" );
  151. 			Console.ForegroundColor = ConsoleColor.White;
  152. 			Console.Error.Write( "M" );
  153. 			Console.ResetColor( );
  154. 			Console.Error.WriteLine( "ake New Folder\" button" );
  155.  
  156. 			Console.Error.WriteLine( "                       (default: hide the button)" );
  157. 			Console.Error.WriteLine( );
  158. 			Console.Error.WriteLine( "Notes:  Though the \"Make New Folder\" button is hidden by default, this does" );
  159. 			Console.Error.WriteLine( "        not inhibit manipulating folders using right-click or Shift+F10." );
  160. 			Console.Error.WriteLine( "        The full path of the selected folder is written to Standard Output" );
  161. 			Console.Error.WriteLine( "        if OK was clicked, or an empty string if Cancel was clicked." );
  162. 			Console.Error.WriteLine( "        The return code will be 0 on success, 1 in case of (command line)" );
  163. 			Console.Error.WriteLine( "        errors, or 2 if Cancel was clicked." );
  164. 			Console.Error.WriteLine( );
  165. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  166. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  167. 			return 1;
  168. 		}
  169. 	}
  170. }
  171.  

page last uploaded: 2017-04-06, 13:33