2008-09-30 43 views

回答

1

使用Bob Kings的想法我寫了下面的方法。

它可以在CD,可移動驅動器,普通驅動器上使用。

然而,對於軟盤它總是返回「不可用」。

public static void TestFloppy(char driveLetter) { 
     using(var searcher = new ManagementObjectSearcher( @"SELECT * FROM Win32_LogicalDisk WHERE DeviceID = '" + driveLetter + ":'")) 
     using(var logicalDisks = searcher.Get()) { 
      foreach(ManagementObject logicalDisk in logicalDisks) { 
       var fs = logicalDisk[ "FreeSpace" ]; 
       Console.WriteLine("FreeSpace = " + (fs ?? "Not Available")); 

       logicalDisk.Dispose(); 
      } 
     } 
    } 
2

這來自Scripting Center @ MSDN

strComputer = "." 
Set objWMIService = GetObject(_ 
    "winmgmts:\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIService.ExecQuery _ 
    ("Select * From Win32_LogicalDisk Where DeviceID = 'A:'") 

For Each objItem in colItems 
    intFreeSpace = objItem.FreeSpace 
    If IsNull(intFreeSpace) Then 
     Wscript.Echo "There is no disk in the floppy drive." 
    Else 
     Wscript.Echo "There is a disk in the floppy drive." 
    End If 
Next 

你也可以知道它是格式化或沒有,通過檢查Win32_LogicalDisk class的其他成員。

+0

不適用於軟盤。請參閱回覆代碼。 – jyoung 2008-10-03 19:20:08

相關問題