Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for runhidden.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7.  
  8.  
  9. namespace RobvanderWoude
  10. {
  11. 	static class RunHidden
  12. 	{
  13. 		static string progver = "1.00";
  14.  
  15. 		[STAThread]
  16. 		static int Main( string[] args )
  17. 		{
  18. 			if ( args.Length == 0 || args[0] == "/?" )
  19. 			{
  20. 				return ShowHelp( );
  21. 			}
  22. 			if ( !File.Exists( args[0] ) && !Which( args[0] ) )
  23. 			{
  24. 				return ShowHelp( String.Format( "Invalid program file \"{0}\"", args[0] ) );
  25. 			}
  26. 			try
  27. 			{
  28. 				int rc = 0;
  29. 				using ( Process process = new Process( ) )
  30. 				{
  31. 					string commandline = Environment.CommandLine;
  32. 					string thisexec = Environment.GetCommandLineArgs( )[0];
  33. 					// Remove RunHidden.exe from command line
  34. 					if ( commandline.StartsWith( thisexec ) )
  35. 					{
  36. 						commandline = commandline.Substring( thisexec.Length ).Trim( );
  37. 					}
  38. 					else
  39. 					{
  40. 						// Assuming doublequotes
  41. 						commandline = commandline.Substring( thisexec.Length + 2 ).Trim( );
  42. 					}
  43. 					process.StartInfo.FileName = Environment.GetEnvironmentVariable( "COMSPEC" );
  44. 					process.StartInfo.Arguments = "/C " + commandline;
  45. 					process.StartInfo.UseShellExecute = false;
  46. 					process.StartInfo.RedirectStandardError = true;
  47. 					process.StartInfo.RedirectStandardInput = true;
  48. 					process.StartInfo.RedirectStandardOutput = true;
  49. 					process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  50. 					process.StartInfo.CreateNoWindow = true;
  51. 					process.Start( );
  52. 					process.WaitForExit( );
  53. 					rc = process.ExitCode;
  54. 				}
  55. 				return rc;
  56. 			}
  57. 			catch ( Exception e )
  58. 			{
  59. 				return ShowHelp( e.Message );
  60. 			}
  61. 		}
  62.  
  63.  
  64. 		static int ShowHelp( string errmsg = "" )
  65. 		{
  66. 			string helptext = String.Empty;
  67. 			if ( !String.IsNullOrWhiteSpace( errmsg ) )
  68. 			{
  69. 				helptext = String.Format( "ERROR:\t{0}\n\n\n", errmsg );
  70. 			}
  71.  
  72. 			helptext += String.Format( "RunHidden.exe,  Version {0}\n", progver );
  73. 			helptext += "Run a console program or script hidden\n\n";
  74. 			helptext += "Usage:\tRUNHIDDEN.EXE  command  [ arguments ]\n\n";
  75. 			helptext += "Where:\tcommand \tis the console program or script to be run\n";
  76. 			helptext += "\targuments \tis/are the optional command line argument(s)\n";
  77. 			helptext += "\t                    \tfor command\n\n";
  78. 			helptext += "Notes:\tUnlike RunNHide.exe, RunHidden.exe will wait for the\n";
  79. 			helptext += "\tspecified command to exit, and pass on its \"errorlevel\".\n";
  80. 			helptext += "\tHowever, like RunNHide.exe, RunHidden.exe runs the specified\n";
  81. 			helptext += "\tcommand in a separate process, so the specified command won't\n";
  82. 			helptext += "\tbe able to change environment variables for its parent process.\n\n";
  83. 			helptext += String.Format( "\tThe specified command is started with {0} /C\n", Path.GetFileName( Environment.GetEnvironmentVariable( "COMSPEC" ) ).ToUpper( ) );
  84. 			helptext += "\tso besides a true executable you can also specify a script file\n";
  85. 			helptext += "\twhich will then be run by its default interpreter. There is no\n";
  86. 			helptext += "\tguarantee, however, that this interpreter will run hidden.\n\n";
  87. 			helptext += "\tRedirection symbols and parentheses in the command line\n";
  88. 			helptext += "\targuments must be escaped with carets, e.g.\n";
  89. 			helptext += "\tRUNHIDDEN DIR ^> dir.log\n";
  90. 			helptext += "\tBetter still: use a \"wrapper\" batch file with the unescaped code.\n\n";
  91. 			helptext += "\tRunHidden.exe returns \"errorlevel\" -1 in case of (command line)\n";
  92. 			helptext += "\terrors, otherwise the specified command's errorlevel is returned.\n\n";
  93. 			helptext += "Written by Rob van der Woude\n";
  94. 			helptext += "http://www.robvanderwoude.com";
  95.  
  96. 			MessageBox.Show( helptext, "Help for RunHidden.exe " + progver );
  97.  
  98. 			return -1;
  99. 		}
  100.  
  101.  
  102. 		static bool Which( string file )
  103. 		{
  104. 			// Insert current directory before PATH and remove empty entries
  105. 			string[] path = String.Format( "{0};{1}", Environment.CurrentDirectory, Environment.GetEnvironmentVariable( "PATH" ) ).Split( ";".ToCharArray( ), StringSplitOptions.RemoveEmptyEntries );
  106. 			// Unlike PATH, do NOT remove empty entries, we REQUIRE the first entry of PATHEXT to be empty
  107. 			string[] pathext = ( ";" + Environment.GetEnvironmentVariable( "PATHEXT" ).ToLower( ) ).Split( ';' );
  108. 			foreach ( string folder in path )
  109. 			{
  110. 				// We assume that PATH does NOT contain UNC paths
  111. 				string dir = ( folder + @"\" ).Replace( @"\\", @"\" );
  112. 				foreach ( string ext in pathext ) // first entry of pathext MUST be empty
  113. 				{
  114. 					{
  115. 						// The EXTERNAL program FILE to be searched MUST have an extension, either specified on the command line or one of the extensions listed in PATHEXT.
  116. 						if ( ( file + ext ).IndexOf( '.' ) > -1 )
  117. 						{
  118. 							if ( File.Exists( dir + file + ext ) )
  119. 							{
  120. 								return true;
  121. 							}
  122. 						}
  123. 					}
  124. 				}
  125. 			}
  126. 			return false;
  127. 		}
  128. 	}
  129. }
  130.  

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