Powered by GeSHi

Source code for isntfs.cs

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

  1. using System;
  2. using System.IO;
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	class IsNTFS
  7. 	{
  8. 		public static int Main( string[] args )
  9. 		{
  10. 			try
  11. 			{
  12. 				if ( args.Length == 0 )
  13. 				{
  14. 					return WriteError( string.Empty );
  15. 				}
  16. 				if ( args.Length > 1 )
  17. 				{
  18. 					return WriteError( "Invalid number of arguments." );
  19. 				}
  20.  
  21. 				string drive = args[0].ToUpper( );
  22.  
  23. 				DriveInfo[] allDrives = DriveInfo.GetDrives( );
  24.  
  25. 				foreach ( DriveInfo drv in allDrives )
  26. 				{
  27. 					if ( drive == drv.Name.Substring( 0, 2 ) )
  28. 					{
  29. 						if ( drv.IsReady )
  30. 						{
  31. 							Console.WriteLine( drv.DriveFormat.ToUpper( ) );
  32. 							if ( drv.DriveFormat == "NTFS" )
  33. 							{
  34. 								return 0;
  35. 							}
  36. 							else
  37. 							{
  38. 								return 2;
  39. 							}
  40. 						}
  41. 						else
  42. 						{
  43. 							Console.WriteLine( drv.DriveType.ToString( ).ToUpper( ) );
  44. 							return 1;
  45. 						}
  46. 					}
  47. 				}
  48. 				return WriteError( "Invalid drive specification." );
  49. 			}
  50. 			catch ( Exception e )
  51. 			{
  52. 				// Display help text with error message
  53. 				return WriteError( e );
  54. 			}
  55. 		}
  56. 		// Code to display help and optional error message, by Bas van der Woude
  57. 		public static int WriteError( Exception e )
  58. 		{
  59. 			return WriteError( e == null ? null : e.Message );
  60. 		}
  61.  
  62. 		public static int WriteError( string errorMessage )
  63. 		{
  64. 			string fullpath = Environment.GetCommandLineArgs( ).GetValue( 0 ).ToString( );
  65. 			string[] program = fullpath.Split( '\\' );
  66. 			string exeName = program[program.GetUpperBound( 0 )];
  67. 			exeName = exeName.Substring( 0, exeName.IndexOf( '.' ) );
  68.  
  69. 			/*
  70. 			IsNTFS,  Version 1.00
  71. 			Return 'errorlevel' 0 if the specified drive is NTFS formated
  72.  
  73. 			Usage:    ISNTFS  drive:
  74.  
  75. 			Note:     Returns 'errorlevel' 0 if NTFS, 2 if not, 1 if not ready or invalid
  76.  
  77. 			Written by Rob van der Woude
  78. 			http://www.robvanderwoude.com
  79. 			*/
  80. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  81. 			{
  82. 				Console.Error.WriteLine( );
  83. 				Console.ForegroundColor = ConsoleColor.Red;
  84. 				Console.Error.Write( "ERROR: " );
  85. 				Console.ForegroundColor = ConsoleColor.White;
  86. 				Console.Error.WriteLine( errorMessage );
  87. 				Console.ResetColor( );
  88. 			}
  89.  
  90. 			Console.Error.WriteLine( );
  91. 			Console.Error.WriteLine( "IsNTFS,  Version 1.00" );
  92. 			Console.Error.WriteLine( "Return 'errorlevel' 0 if the specified drive is NTFS formated" );
  93. 			Console.Error.WriteLine( );
  94. 			Console.Error.Write( "Usage:    " );
  95. 			Console.ForegroundColor = ConsoleColor.White;
  96. 			Console.Error.WriteLine( "{0}  drive:", exeName.ToUpper( ) );
  97. 			Console.ResetColor( );
  98. 			Console.Error.WriteLine( );
  99. 			Console.Error.WriteLine( "Note:     Returns 0 if NTFS, 2 if not, 1 if not ready or invalid." );
  100. 			Console.Error.WriteLine( );
  101. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  102. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  103. 			return 1;
  104. 		}
  105. 	}
  106. }