Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for runnhide.cs

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

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	static class RunNHide
  10. 	{
  11. 		static string progver = "1.01";
  12. 		static string currentdir = Directory.GetCurrentDirectory( );
  13. 		static string arguments = String.Empty;
  14. 		static int rc = 0;
  15.  
  16.  
  17. 		[STAThread]
  18. 		static int Main( string[] args )
  19. 		{
  20. 			if ( args.Length == 0 || args[0] == "/?" )
  21. 			{
  22. 				return ShowHelp( );
  23. 			}
  24. 			else
  25. 			{
  26. 				string exec = args[0];
  27. 				if ( args.Length > 1 )
  28. 				{
  29. 					string arguments = Environment.CommandLine;
  30. 					string thisexec = Environment.GetCommandLineArgs( )[0];
  31. 					// Remove RunNHide.exe from command line
  32. 					if ( arguments.StartsWith( thisexec ) )
  33. 					{
  34. 						arguments = arguments.Substring( thisexec.Length ).Trim( );
  35. 					}
  36. 					else
  37. 					{
  38. 						// Assuming doublequotes
  39. 						arguments = arguments.Substring( thisexec.Length + 2 ).Trim( );
  40. 					}
  41. 					// Remove specified program name from command line
  42. 					if ( arguments.StartsWith( exec ) )
  43. 					{
  44. 						arguments = arguments.Substring( exec.Length ).Trim( );
  45. 					}
  46. 					else
  47. 					{
  48. 						// Assuming doublequotes
  49. 						arguments = arguments.Substring( exec.Length + 2 ).Trim( );
  50. 					}
  51. 				}
  52. 				exec = GetFullPath( exec );
  53. 				try
  54. 				{
  55. 					ProcessStartInfo psi = new ProcessStartInfo( );
  56. 					if ( Path.GetExtension( exec ).ToLower( ) == ".exe" )
  57. 					{
  58. 						psi.FileName = exec;
  59. 						psi.Arguments = arguments;
  60. 					}
  61. 					else
  62. 					{
  63. 						psi.FileName = Environment.GetEnvironmentVariable( "COMSPEC" );
  64. 						psi.Arguments = String.Format( "/C START /B /MIN \"\" \"{0}\" {1}", exec, arguments );
  65. 					}
  66. 					psi.CreateNoWindow = true;
  67. 					psi.LoadUserProfile = false;
  68. 					psi.RedirectStandardError = false;
  69. 					psi.RedirectStandardOutput = false;
  70. 					psi.UseShellExecute = false;
  71. 					psi.WindowStyle = ProcessWindowStyle.Hidden;
  72. 					psi.WorkingDirectory = currentdir;
  73. 					Process process = new Process( );
  74. 					process.StartInfo = psi;
  75. 					process.Start( );
  76. 					return rc;
  77. 				}
  78. 				catch ( Exception e )
  79. 				{
  80. 					MessageBox.Show( e.ToString( ) );
  81. 					return ShowHelp( e.Message );
  82. 				}
  83. 			}
  84. 		}
  85.  
  86.  
  87. 		static string GetFullPath( string filename )
  88. 		{
  89. 			string fullpath = filename;
  90. 			bool execfound = false;
  91. 			if ( File.Exists( fullpath ) )
  92. 			{
  93. 				fullpath = Path.GetFullPath( fullpath );
  94. 				execfound = true;
  95. 			}
  96. 			else
  97. 			{
  98. 				string[] path = ( currentdir + ";" + Environment.GetEnvironmentVariable( "PATH" ) ).Split( ";".ToCharArray( ) );
  99. 				string[] pathext = ( ";" + Environment.GetEnvironmentVariable( "PATHEXT" ) ).Split( ";".ToCharArray( ) );
  100. 				if ( !execfound )
  101. 				{
  102. 					foreach ( string dir in path )
  103. 					{
  104. 						if ( !execfound )
  105. 						{
  106. 							foreach ( string ext in pathext )
  107. 							{
  108. 								if ( !execfound )
  109. 								{
  110. 									if ( File.Exists( Path.Combine( dir, fullpath + ext ) ) )
  111. 									{
  112. 										fullpath = Path.Combine( dir, fullpath + ext );
  113. 										execfound = true;
  114. 									}
  115. 								}
  116. 							}
  117. 						}
  118. 					}
  119. 				}
  120. 			}
  121. 			if ( !execfound ) // might be internal command or file association
  122. 			{
  123. 				arguments = String.Format( "/C START /B /MIN \"\" \"{0}\" {1}", fullpath, arguments );
  124. 				fullpath = Environment.GetEnvironmentVariable( "COMSPEC" );
  125. 				rc = 2;
  126. 			}
  127. 			return fullpath;
  128. 		}
  129.  
  130.  
  131. 		static int ShowHelp( params string[] args )
  132. 		{
  133. 			string title = "Help for RunNHide " + progver;
  134. 			string helptext = String.Empty;
  135. 			if ( args.Length > 0 )
  136. 			{
  137. 				helptext += String.Format( "ERROR:\t{0}\n\n", args );
  138. 				title = "RunNHide Error";
  139. 			}
  140. 			helptext += String.Format( "RunNHide,  Version {0}\n", progver );
  141. 			helptext += "Start a console program or script in a hidden window\n\n";
  142. 			helptext += "Usage:\tRUNNHIDE    command    [ arguments ]\n\n";
  143. 			helptext += "Where:\tcommand \tis the console program or script to be run\n";
  144. 			helptext += "\targuments \tis/are the optional command line\n";
  145. 			helptext += "\t                    \targument(s) for command\n\n";
  146. 			helptext += "Notes:\t\"command\" will be started in a separate process, so catching\n";
  147. 			helptext += "\terrors or the command's errorlevel, or changing environment\n";
  148. 			helptext += "\tvariables is not possible; run \"command\" in a \"wrapper\" batch\n";
  149. 			helptext += "\tfile to add your own cusom error handling.\n\n";
  150. 			helptext += "\tRedirection symbols and parentheses in the command line\n";
  151. 			helptext += "\targuments must be escaped with carets, e.g.\n\tRUNNHIDE DIR ^> dir.log\n";
  152. 			helptext += "\tBetter still: use a \"wrapper\" batch file with the unescaped code.\n\n";
  153. 			helptext += "\tIf \"command\" is a file with a registered file association, it will be\n";
  154. 			helptext += "\tstarted with the standard command interpreter:\n";
  155. 			helptext += String.Format( "\t{0} /C START /B /MIN \"command\" [ arguments ]\n", Path.GetFileName( Environment.GetEnvironmentVariable( "COMSPEC" ) ).ToUpper( ) );
  156. 			helptext += "\tThis will in turn start the file in its associated program; however,\n";
  157. 			helptext += "\tthere is no guarantee that the associated program will run hidden.\n\n";
  158. 			helptext += "\tIf \"command\" cannot be found in the PATH, not even after\n";
  159. 			helptext += "\tappending extension found in PATHEXT, RunNHide.exe will\n";
  160. 			helptext += "\ttry and start it using the standard command interpreter,\n";
  161. 			helptext += "\tlike it does for registered file types.\n\n";
  162. 			helptext += "\tRunNHide.exe will return errorlevel 1 in case of detected errors,\n";
  163. 			helptext += "\tor 2 if the command file could not be found, or 0 otherwise.\n\n";
  164. 			helptext += "Written by Rob van der Woude\nhttp://www.robvanderwoude.com";
  165. 			MessageBoxButtons button = MessageBoxButtons.OK;
  166. 			MessageBoxIcon icon = MessageBoxIcon.None;
  167. 			MessageBox.Show( helptext, title, button, icon );
  168. 			return 1;
  169. 		}
  170. 	}
  171. }
  172.  

page last uploaded: 2017-07-06, 12:37