欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#判断指定分区是否是ntfs格式的方法

程序员文章站 2023-10-26 23:57:22
本文实例讲述了c#判断指定分区是否是ntfs格式的方法。分享给大家供大家参考。具体如下: using system; using system.io; nam...

本文实例讲述了c#判断指定分区是否是ntfs格式的方法。分享给大家供大家参考。具体如下:

using system;
using system.io;
namespace robvanderwoude
{
 class isntfs
 {
  public static int main( string[] args )
  {
   try
   {
    if ( args.length == 0 )
    {
     return writeerror( string.empty );
    }
    if ( args.length > 1 )
    {
     return writeerror( "invalid number of arguments." );
    }
     string drive = args[0].toupper( );
     driveinfo[] alldrives = driveinfo.getdrives( );
     foreach ( driveinfo drv in alldrives )
    {
     if ( drive == drv.name.substring( 0, 2 ) )
     {
      if ( drv.isready )
      {
       console.writeline( drv.driveformat.toupper( ) );
       if ( drv.driveformat == "ntfs" )
       {
        return 0;
       }
       else
       {
        return 2;
       }
      }
      else
      {
       console.writeline( drv.drivetype.tostring( ).toupper( ) );
       return 1;
      }
     }
    }
    return writeerror( "invalid drive specification." );
   }
   catch ( exception e )
   {
    // display help text with error message
    return writeerror( e );
   }
  }
  // code to display help and optional error message, 
  //by bas van der woude
  public static int writeerror( exception e )
  {
   return writeerror( e == null ? null : e.message );
  }
   public static int writeerror( string errormessage )
  {
   string fullpath = environment.getcommandlineargs().getvalue(0).tostring();
   string[] program = fullpath.split( '\\' );
   string exename = program[program.getupperbound( 0 )];
   exename = exename.substring( 0, exename.indexof( '.' ) );
    if ( string.isnullorempty( errormessage ) == false )
   {
    console.error.writeline( );
    console.foregroundcolor = consolecolor.red;
    console.error.write( "error: " );
    console.foregroundcolor = consolecolor.white;
    console.error.writeline( errormessage );
    console.resetcolor( );
   }
    console.error.writeline( );
   console.error.writeline( "isntfs, version 1.00" );
   console.error.writeline( "return 'errorlevel' 0 if the specified drive is ntfs formated" );
   console.error.writeline( );
   console.error.write( "usage:  " );
   console.foregroundcolor = consolecolor.white;
   console.error.writeline( "{0} drive:", exename.toupper( ) );
   console.resetcolor( );
   console.error.writeline( );
   console.error.writeline( "note:   returns 0 if ntfs, 2 if not, 1 if not ready or invalid." );
   console.error.writeline( );
   console.error.writeline( "written by rob van der woude" );
   return 1;
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。