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.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class OpenFolderBox
  10. 	{
  11. 		static string progver = "1.02";
  12.  
  13. 		[STAThread]
  14. 		static int Main( string[] args )
  15. 		{
  16. 			try
  17. 			{
  18. 				using ( FolderBrowserDialog dialog = new FolderBrowserDialog( ) )
  19. 				{
  20. 					string startfolder = Directory.GetCurrentDirectory( );
  21. 					string description = String.Format( "OpenFolderBox,  Version {0}", progver );
  22. 					bool startfolderset = false;
  23. 					bool descriptionset = false;
  24. 					bool allowmakedir = false;
  25.  
  26. 					if ( args.Length > 3 )
  27. 					{
  28. 						return ShowHelp( "Too many command line arguments" );
  29. 					}
  30.  
  31. 					if ( args.Length > 0 )
  32. 					{
  33. 						foreach ( string arg in args )
  34. 						{
  35. 							switch ( arg.ToUpper( ) )
  36. 							{
  37. 								case "/?":
  38. 									return ShowHelp( );
  39. 								case "/MD":
  40. 									allowmakedir = true;
  41. 									break;
  42. 								default:
  43. 									if ( startfolderset )
  44. 									{
  45. 										if ( descriptionset )
  46. 										{
  47. 											return ShowHelp( "Invalid or duplicate command line argument \"{0}\"", arg );
  48. 										}
  49. 										description = arg.Replace( "\\n", "\n" ).Replace( "\\t", "\t" );
  50. 										descriptionset = true;
  51. 									}
  52. 									else
  53. 									{
  54. 										startfolder = Path.GetFullPath( arg );
  55. 										startfolderset = true;
  56. 										if ( !Directory.Exists( startfolder ) )
  57. 										{
  58. 											return ShowHelp( "Invalid folder \"{0}\"", startfolder );
  59. 										}
  60. 									}
  61. 									break;
  62. 							}
  63. 						}
  64. 					}
  65. 					dialog.SelectedPath = startfolder;
  66. 					dialog.Description = description;
  67. 					dialog.ShowNewFolderButton = allowmakedir;
  68. 					if ( dialog.ShowDialog( ) == DialogResult.OK )
  69. 					{
  70. 						Console.WriteLine( dialog.SelectedPath );
  71. 						return 0;
  72. 					}
  73. 					else
  74. 					{
  75. 						// Cancel was clicked
  76. 						return 2;
  77. 					}
  78. 				}
  79. 			}
  80. 			catch ( Exception e )
  81. 			{
  82. 				return ShowHelp( e.Message );
  83. 			}
  84. 		}
  85.  
  86. 		static int ShowHelp( params string[] errmsg )
  87. 		{
  88. 			/*
  89. 			OpenFolderBox.exe,  Version 1.02
  90. 			Batch tool to present a Browse Folders Dialog and return the selected path
  91.  
  92. 			Usage:  OPENFOLDERBOX  [ "startfolder"  [ "description" ] ]  [ /MD ]
  93.  
  94. 			Where:  "startfolder"  is the initial folder the dialog will show on opening
  95. 			                       (default: current directory)
  96. 			        "description"  is the text above the dialog's tree view
  97. 			                       (default: program name and version)
  98. 			        /MD            display the "Make New Folder" button
  99. 			                       (default: hide the button)
  100.  
  101. 			Notes:  Though the "Make New Folder" button is hidden by default, this does
  102. 			        not inhibit manipulating folders using right-click or Shift+F10.
  103. 			        The full path of the selected folder is written to Standard Output
  104. 			        if OK was clicked, or an empty string if Cancel was clicked.
  105. 			        The return code will be 0 on success, 1 in case of (command line)
  106. 			        errors, or 2 if Cancel was clicked.
  107. 			        Warning: The startfolder path should never end with a trailing
  108. 			                 backslash followed by a doublequote, as this would be
  109. 			                 interpreted as an escaped doublequote; to prevent this,
  110. 			                 either remove trailing backslashes, or escape it (\\),
  111. 			                 or append a dot to the startfolder path, or remove the
  112. 			                 doublequotes and use the short (8.3) notation for the path.
  113.  
  114. 			Written by Rob van der Woude
  115. 			http://www.robvanderwoude.com
  116. 			*/
  117.  
  118. 			#region Error Message
  119.  
  120. 			if ( errmsg.Length > 0 )
  121. 			{
  122. 				List<string> errargs = new List<string>( errmsg );
  123. 				errargs.RemoveAt( 0 );
  124. 				Console.Error.WriteLine( );
  125. 				Console.ForegroundColor = ConsoleColor.Red;
  126. 				Console.Error.Write( "ERROR:\t" );
  127. 				Console.ForegroundColor = ConsoleColor.White;
  128. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  129. 				Console.ResetColor( );
  130. 			}
  131.  
  132. 			#endregion Error Message
  133.  
  134. 			Console.Error.WriteLine( );
  135.  
  136. 			Console.Error.WriteLine( "OpenFolderBox.exe,  Version {0}", progver );
  137.  
  138. 			Console.Error.WriteLine( "Batch tool to present a Browse Folders Dialog and return the selected path" );
  139.  
  140. 			Console.Error.WriteLine( );
  141.  
  142. 			Console.Error.Write( "Usage:  " );
  143. 			Console.ForegroundColor = ConsoleColor.White;
  144. 			Console.Error.WriteLine( "OPENFOLDERBOX  [ \"startfolder\"  [ \"description\" ] ]  [ /MD ]" );
  145. 			Console.ResetColor( );
  146.  
  147. 			Console.Error.WriteLine( );
  148.  
  149. 			Console.Error.Write( "Where:  " );
  150. 			Console.ForegroundColor = ConsoleColor.White;
  151. 			Console.Error.Write( "\"startfolder\"" );
  152. 			Console.ResetColor( );
  153. 			Console.Error.WriteLine( "  is the initial folder the dialog will show on opening" );
  154.  
  155. 			Console.Error.WriteLine( "                       (default: current directory)" );
  156.  
  157. 			Console.ForegroundColor = ConsoleColor.White;
  158. 			Console.Error.Write( "        \"description\"" );
  159. 			Console.ResetColor( );
  160. 			Console.Error.WriteLine( "  is the text above the dialog's tree view" );
  161.  
  162. 			Console.Error.WriteLine( "                       (default: \"OpenFolderBox,  Version {0}\")", progver );
  163.  
  164. 			Console.ForegroundColor = ConsoleColor.White;
  165. 			Console.Error.Write( "        /MD" );
  166. 			Console.ResetColor( );
  167. 			Console.Error.Write( "            display the \"" );
  168. 			Console.ForegroundColor = ConsoleColor.White;
  169. 			Console.Error.Write( "M" );
  170. 			Console.ResetColor( );
  171. 			Console.Error.WriteLine( "ake New Folder\" button" );
  172.  
  173. 			Console.Error.WriteLine( "                       (default: hide the button)" );
  174.  
  175. 			Console.Error.WriteLine( );
  176.  
  177. 			Console.Error.WriteLine( "Notes:  Though the \"Make New Folder\" button is hidden by default, this does" );
  178.  
  179. 			Console.Error.WriteLine( "        not inhibit manipulating folders using right-click or Shift+F10." );
  180.  
  181. 			Console.Error.WriteLine( "        The full path of the selected folder is written to Standard Output" );
  182.  
  183. 			Console.Error.WriteLine( "        if OK was clicked, or an empty string if Cancel was clicked." );
  184.  
  185. 			Console.Error.WriteLine( "        The return code will be 0 on success, 1 in case of (command line)" );
  186.  
  187. 			Console.Error.WriteLine( "        errors, or 2 if Cancel was clicked." );
  188.  
  189. 			Console.ForegroundColor = ConsoleColor.White;
  190. 			Console.Error.Write( "        Warning:" );
  191. 			Console.ResetColor( );
  192. 			Console.Error.Write( " The " );
  193. 			Console.ForegroundColor = ConsoleColor.White;
  194. 			Console.Error.Write( "startfolder" );
  195. 			Console.ResetColor( );
  196. 			Console.Error.Write( " path should " );
  197. 			Console.ForegroundColor = ConsoleColor.White;
  198. 			Console.Error.Write( "never" );
  199. 			Console.ResetColor( );
  200. 			Console.Error.WriteLine( " end with a trailing" );
  201.  
  202. 			Console.Error.WriteLine( "                 backslash followed by a doublequote, as this would be" );
  203.  
  204. 			Console.Error.WriteLine( "                 interpreted as an escaped doublequote; to prevent this," );
  205.  
  206. 			Console.Error.WriteLine( "                 either remove trailing backslashes, or escape it (\\\\)," );
  207.  
  208. 			Console.Error.Write( "                 or append a dot to the " );
  209. 			Console.ForegroundColor = ConsoleColor.White;
  210. 			Console.Error.Write( "startfolder" );
  211. 			Console.ResetColor( );
  212. 			Console.Error.WriteLine( " path, or remove the" );
  213.  
  214. 			Console.Error.WriteLine( "                 doublequotes and use the short (8.3) notation for the path." );
  215.  
  216. 			Console.Error.WriteLine( );
  217.  
  218. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  219.  
  220. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  221.  
  222. 			return 1;
  223. 		}
  224. 	}
  225. }
  226.  

page last uploaded: 2017-08-21, 14:26