C#判断指定驱动器是否是Fat分区格式的方法
本文实例讲述了C#判断指定驱动器是否是Fat分区格式的方法。分享给大家供大家参考。具体如下:
usingSystem;
usingSystem.IO;
namespaceRobvanderWoude
{
classIsFAT
{
publicstaticintMain(string[]args)
{
try
{
if(args.Length==0)
{
returnWriteError(string.Empty);
}
if(args.Length>1)
{
returnWriteError("Invalidnumberofarguments.");
}
stringdrive=args[0].ToUpper();
DriveInfo[]allDrives=DriveInfo.GetDrives();
foreach(DriveInfodrvinallDrives)
{
if(drive==drv.Name.Substring(0,2))
{
if(drv.IsReady)
{
Console.WriteLine(drv.DriveFormat.ToUpper());
if(drv.DriveFormat=="FAT"||drv.DriveFormat=="FAT32")
{
return0;
}
else
{
return2;
}
}
else
{
Console.WriteLine(drv.DriveType.ToString().ToUpper());
return1;
}
}
}
returnWriteError("Invaliddrivespecification.");
}
catch(Exceptione)
{
//Displayhelptextwitherrormessage
returnWriteError(e);
}
}
//Codetodisplayhelpandoptionalerrormessage,
//byBasvanderWoude
publicstaticintWriteError(Exceptione)
{
returnWriteError(e==null?null:e.Message);
}
publicstaticintWriteError(stringerrorMessage)
{
stringfullpath=Environment.GetCommandLineArgs().GetValue(0).ToString();
string[]program=fullpath.Split('\\');
stringexeName=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("IsFAT,Version1.00");
Console.Error.WriteLine("Return'errorlevel'0ifthespecifieddriveisFATorFAT32formated");
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:Returns0ifFATorFAT32,2ifnot,1ifnotreadyorinvalid.");
Console.Error.WriteLine();
Console.Error.WriteLine("WrittenbyRobvanderWoude");
return1;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。