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

page last modified: 2024-04-16; loaded in 0.0067 seconds