Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bootstate.cs

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

  1. using System;
  2. using System.Management;
  3. using Microsoft.Win32;
  4.  
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class BootState
  9. 	{
  10. 		static string progver = "1.00";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			if ( args.Length > 0 )
  16. 			{
  17. 				return ShowHelp( );
  18. 			}
  19.  
  20. 			// Check for Windows PE first
  21. 			foreach ( string regkey in Registry.LocalMachine.OpenSubKey( @"SYSTEM\ControlSet001\Control" ).GetSubKeyNames( ) )
  22. 			{
  23. 				if ( regkey.ToUpper( ) == "MININT" )
  24. 				{
  25. 					Console.Write( "Windows PE" );
  26. 					return 3;
  27. 				}
  28. 			}
  29.  
  30. 			// Check "regular" boot modes
  31. 			string[] bootupstates = new string[] { "Normal boot", "Fail-safe boot", "Fail-safe with network boot" };
  32. 			ManagementObjectSearcher searcher = new ManagementObjectSearcher( "SELECT BootupState FROM Win32_ComputerSystem" );
  33. 			foreach ( ManagementObject item in searcher.Get( ) )
  34. 			{
  35. 				string bootupstate = item["BootupState"].ToString( );
  36. 				Console.Write( bootupstate.Replace( "Fail-safe", "Safe mode" ).Replace( " boot", "" ) );
  37. 				for ( int i = 0; i < bootupstates.Length; i++ )
  38. 				{
  39. 					if ( bootupstate.ToUpper( ) == bootupstates[i].ToUpper( ) )
  40. 					{
  41. 						return i;
  42. 					}
  43. 				}
  44. 			}
  45.  
  46. 			// Return error if boot mode not determined
  47. 			Console.Write( "Unknown" );
  48. 			return -1;
  49. 		}
  50.  
  51.  
  52. 		static int ShowHelp( )
  53. 		{
  54. 			Console.Error.WriteLine( );
  55. 			Console.Error.WriteLine( "BootState.exe,  Version {0}", progver );
  56. 			Console.Error.WriteLine( "Show Windows' boot state" );
  57. 			Console.Error.WriteLine( );
  58. 			Console.Error.WriteLine( "Usage:    BootState.exe" );
  59. 			Console.Error.WriteLine( );
  60. 			Console.Error.WriteLine( "Notes:    Boot state is returned both as a string and as return code:" );
  61. 			Console.Error.WriteLine( "              \"Normal\"                    (rc = 0)" );
  62. 			Console.Error.WriteLine( "              \"Safe mode\"                 (rc = 1)" );
  63. 			Console.Error.WriteLine( "              \"Safe mode with network\"    (rc = 2)" );
  64. 			Console.Error.WriteLine( "              \"Windows PE\"                (rc = 3)" );
  65. 			Console.Error.WriteLine( "          In case of (command line) errors, the return code will be -1." );
  66. 			Console.Error.WriteLine( );
  67. 			Console.Error.WriteLine( "Credits:  Windows PE detection based on a tip by Mitch Tulloch" );
  68. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  69. 			Console.Error.WriteLine( "          http://techgenix.com/HowtodetectwhetheryouareinWindowsPE/" );
  70. 			Console.ResetColor( );
  71. 			Console.Error.WriteLine( );
  72. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  73. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  74.  
  75. 			return -1;
  76. 		}
  77. 	}
  78. }
  79.  

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