Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for openfilebox.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class OpenFile
  10. 	{
  11. 		static string progver = "1.01";
  12.  
  13. 		[STAThread]
  14. 		static int Main( string[] args )
  15. 		{
  16. 			try
  17. 			{
  18. 				using ( OpenFileDialog dialog = new OpenFileDialog( ) )
  19. 				{
  20. 					string filter = "All files (*.*)|*.*";
  21. 					string folder = Directory.GetCurrentDirectory( );
  22. 					string title = "OpenFileBox,  Version " + progver;
  23. 					if ( args.Length > 3 )
  24. 					{
  25. 						return WriteError( "Too many command line arguments" );
  26. 					}
  27. 					if ( args.Length > 0 )
  28. 					{
  29. 						filter = args[0];
  30. 						if ( filter == "/?" )
  31. 						{
  32. 							return WriteError( );
  33. 						}
  34. 						// If only "*.ext" is specified, use "ext files (*.ext)|*.ext" instead
  35. 						if ( Regex.IsMatch( filter, @"^\*\.(\*|\w+)$" ) )
  36. 						{
  37. 							string ext = filter.Substring( 2 ).ToLower( );
  38. 							if ( ext == "*" )
  39. 							{
  40. 								filter = "All files (*." + ext + ")|*." + ext;
  41. 							}
  42. 							else
  43. 							{
  44. 								filter = ext + " files (*." + ext + ")|*." + ext;
  45. 							}
  46. 						}
  47. 						// Append "All files" filter if not specified
  48. 						if ( !Regex.IsMatch( filter, @"All files\s+\(\*\.\*\)\|\*\.\*", RegexOptions.IgnoreCase ) )
  49. 						{
  50. 							if ( String.IsNullOrWhiteSpace( filter ) )
  51. 							{
  52. 								filter = "All files (*.*)|*.*";
  53. 							}
  54. 							else
  55. 							{
  56. 								filter = filter + "|All files (*.*)|*.*";
  57. 							}
  58. 						}
  59. 						// Optional second command line argument is start folder
  60. 						if ( args.Length > 1 )
  61. 						{
  62. 							folder = args[1];
  63. 							if ( !Directory.Exists( folder ) )
  64. 							{
  65. 								return WriteError( "Invalid folder \"" + folder + "\"" );
  66. 							}
  67. 							// Optional third command line argument is dialog title
  68. 							if ( args.Length > 2 )
  69. 							{
  70. 								title = args[2];
  71. 							}
  72. 						}
  73. 					}
  74. 					dialog.Filter = filter;
  75. 					dialog.FilterIndex = 1;
  76. 					dialog.InitialDirectory = folder;
  77. 					dialog.Title = title;
  78. 					dialog.RestoreDirectory = true;
  79. 					if ( dialog.ShowDialog( ) == DialogResult.OK )
  80. 					{
  81. 						Console.WriteLine( dialog.FileName );
  82. 						return 0;
  83. 					}
  84. 					else
  85. 					{
  86. 						// Cancel was clicked
  87. 						return 2;
  88. 					}
  89. 				}
  90. 			}
  91. 			catch ( Exception e )
  92. 			{
  93. 				return WriteError( e.Message );
  94. 			}
  95. 		}
  96.  
  97. 		public static int WriteError( string errorMessage = null )
  98. 		{
  99. 			/*
  100. 			OpenFileBox.exe,  Version 1.01
  101. 			Batch tool to present an Open File Dialog and return the selected file path
  102.  
  103. 			Usage:  OPENFILEBOX  [ "filetypes"  [ "startfolder"  [ "title" ] ] ]
  104.  
  105. 			Where:  filetypes    file type(s) in format "description (*.ext)|*.ext"
  106. 			                     or just "*.ext" (default: "All files (*.*)|*.*")
  107. 			        startfolder  the initial folder the dialog will show on opening
  108. 			                     (default: current directory)
  109. 			        title        the caption in the dialog's title bar
  110. 			                     (default: program name and version)
  111.  
  112. 			Notes:  Multiple file types can be used for the filetypes filter; use "|" as a
  113. 			        separator, e.g. "PDF files (*.pdf)|*.txt|Word documents (*.doc)|*.doc".
  114. 			        If the filetypes filter is in "*.ext" format, "ext files (*.ext)|*.ext"
  115. 			        will be used instead.
  116. 			        Unless the filetypes filter specified is "All files (*.*)|*.*" or
  117. 			        "*.*", the filetypes filter "|All files (*.*)|*.*" will be appended.
  118. 			        The full path of the selected file is written to Standard Output
  119. 			        if OK was clicked, or an empty string if Cancel was clicked.
  120. 			        The return code will be 0 on success, 1 in case of (command line)
  121. 			        errors, or 2 if Cancel was clicked.
  122.  
  123. 			Written by Rob van der Woude
  124. 			http://www.robvanderwoude.com
  125. 			*/
  126.  
  127. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  128. 			{
  129. 				Console.Error.WriteLine( );
  130. 				Console.ForegroundColor = ConsoleColor.Red;
  131. 				Console.Error.Write( "ERROR: " );
  132. 				Console.ForegroundColor = ConsoleColor.White;
  133. 				Console.Error.WriteLine( errorMessage );
  134. 				Console.ResetColor( );
  135. 			}
  136. 			Console.Error.WriteLine( );
  137. 			Console.Error.WriteLine( "OpenFileBox.exe,  Version {0}", progver );
  138. 			Console.Error.WriteLine( "Batch tool to present an Open File Dialog and return the selected file path" );
  139. 			Console.Error.WriteLine( );
  140. 			Console.Error.Write( "Usage:  " );
  141. 			Console.ForegroundColor = ConsoleColor.White;
  142. 			Console.Error.WriteLine( "OPENFILEBOX  [ \"filetypes\"  [ \"startfolder\"  [ \"title\" ] ] ]" );
  143. 			Console.ResetColor( );
  144. 			Console.Error.WriteLine( );
  145. 			Console.Error.Write( "Where:  " );
  146. 			Console.ForegroundColor = ConsoleColor.White;
  147. 			Console.Error.Write( "filetypes" );
  148. 			Console.ResetColor( );
  149. 			Console.Error.Write( "    file type(s) in format " );
  150. 			Console.ForegroundColor = ConsoleColor.White;
  151. 			Console.Error.WriteLine( "\"description (*.ext)|*.ext\"" );
  152. 			Console.ResetColor( );
  153. 			Console.Error.Write( "                     or just " );
  154. 			Console.ForegroundColor = ConsoleColor.White;
  155. 			Console.Error.Write( "\"*.ext\"" );
  156. 			Console.ResetColor( );
  157. 			Console.Error.Write( " (default: " );
  158. 			Console.ForegroundColor = ConsoleColor.White;
  159. 			Console.Error.Write( "\"All files (*.*)|*.*\"" );
  160. 			Console.ResetColor( );
  161. 			Console.Error.WriteLine( ")" );
  162. 			Console.ForegroundColor = ConsoleColor.White;
  163. 			Console.Error.Write( "        startfolder" );
  164. 			Console.ResetColor( );
  165. 			Console.Error.WriteLine( "  the initial folder the dialog will show on opening" );
  166. 			Console.Error.WriteLine( "                     (default: current directory)" );
  167. 			Console.ForegroundColor = ConsoleColor.White;
  168. 			Console.Error.Write( "        title" );
  169. 			Console.ResetColor( );
  170. 			Console.Error.WriteLine( "        the caption in the dialog's title bar" );
  171. 			Console.Error.WriteLine( "                     (default: \"OpenFileBox,  Version {0})\"", progver );
  172. 			Console.Error.WriteLine( );
  173. 			Console.Error.WriteLine( "Notes:  Multiple file types can be used for the filetypes filter; use \"|\" as a" );
  174. 			Console.Error.Write( "        separator, e.g. " );
  175. 			Console.ForegroundColor = ConsoleColor.White;
  176. 			Console.Error.WriteLine( "\"PDF files (*.pdf)|*.txt|Word documents (*.doc)|*.doc\"." );
  177. 			Console.ResetColor( );
  178. 			Console.Error.Write( "        If the filetypes filter is in " );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Error.Write( "\"*.ext\"" );
  181. 			Console.ResetColor( );
  182. 			Console.Error.Write( " format, " );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.WriteLine( "\"ext files (*.ext)|*.ext\"" );
  185. 			Console.ResetColor( );
  186. 			Console.Error.WriteLine( "        will be used instead." );
  187. 			Console.Error.Write( "        Unless the filetypes filter specified is " );
  188. 			Console.ForegroundColor = ConsoleColor.White;
  189. 			Console.Error.Write( "\"All files (*.*)|*.*\"" );
  190. 			Console.ResetColor( );
  191. 			Console.Error.WriteLine( " or" );
  192. 			Console.ForegroundColor = ConsoleColor.White;
  193. 			Console.Error.Write( "        \"*.*\"" );
  194. 			Console.ResetColor( );
  195. 			Console.Error.Write( ", the filetypes filter " );
  196. 			Console.ForegroundColor = ConsoleColor.White;
  197. 			Console.Error.Write( "\"|All files (*.*)|*.*\"" );
  198. 			Console.ResetColor( );
  199. 			Console.Error.WriteLine( " will be appended." );
  200. 			Console.Error.WriteLine( "        The full path of the selected file is written to Standard Output" );
  201. 			Console.Error.WriteLine( "        if OK was clicked, or an empty string if Cancel was clicked." );
  202. 			Console.Error.WriteLine( "        The return code will be 0 on success, 1 in case of (command line)" );
  203. 			Console.Error.WriteLine( "        errors, or 2 if Cancel was clicked." );
  204. 			Console.Error.WriteLine( );
  205. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  206. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  207. 			return 1;
  208. 		}
  209. 	}
  210. }
  211.  

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