如何獲取PC中的所有驅動器。和類型的每個如何使用C#使用.NET獲得PC中的所有驅動器
即系統驅動器,CD驅動器,DVD驅動器,可移動,...等
每個驅動器和自由空間的如果系統上安裝有新的驅動器筆式驅動器或外部硬盤。
如何在附件時檢測它們?
如何獲取PC中的所有驅動器。和類型的每個如何使用C#使用.NET獲得PC中的所有驅動器
即系統驅動器,CD驅動器,DVD驅動器,可移動,...等
每個驅動器和自由空間的如果系統上安裝有新的驅動器筆式驅動器或外部硬盤。
如何在附件時檢測它們?
要獲得驅動器的列表,你可以使用System.IO.DriveInfo類:
foreach(var drive in DriveInfo.GetDrives())
{
Console.WriteLine("Drive Type: {0}", drive.DriveType);
Console.WriteLine("Drive Size: {0}", drive.TotalSize);
Console.WriteLine("Drive Free Space: {0}", drive.TotalFreeSpace);
}
不幸的是,這並沒有提供一種方式來傾聽USB鑰匙的插入。還有另外一個問題處理,你可以看看:
Environment.GetLogicalDrives();
Environment.GetLogicalDrives();
這裏是回答你的第一個問題。要獲得所有的邏輯驅動器,你可以試試這個:
string[] drives = Environment.GetLogicalDrives();
System.IO.DriveInfo
類是開始的地方。 DriveType屬性可以告訴你你在找什麼。
要獲得當前連接的所有驅動器:
DriveInfo[] allDrives = DriveInfo.GetDrives();
而且用於檢測你必須聽WMI事件。檢查了這一點:http://www.techtalkz.com/c-c-sharp/182048-need-detect-insertion-removal-usb-drive.html
你可以在驅動器和信息很容易
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
Console.WriteLine(drive.Name);
Console.WriteLine(drive.TotalSize);
}
有在檢測添加/刪除驅動器on CodeProject
的WMI libraries可能會幫助一個很好的文章。也有談到有關這個CodeProject上的文章:
public string getalldrivestotalnfreespace()
{
string s = " Drive Free Space TotalSpace FileSystem %Free Space DriveType\n\r========================================================================================\n\r";
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
double ts = 0;
double fs = 0;
double frprcntg = 0;
long divts = 1024 * 1024 * 1024;
long divfs = 1024 * 1024 * 1024;
string tsunit = "GB";
string fsunit = "GB";
if (drive.IsReady)
{
fs = drive.TotalFreeSpace;
ts = drive.TotalSize;
frprcntg = (fs/ts) * 100;
if (drive.TotalSize < 1024)
{
divts =1; tsunit = "Byte(s)";
}
else if (drive.TotalSize < (1024*1024))
{
divts = 1024; tsunit = "KB";
}
else if (drive.TotalSize < (1024 * 1024*1024))
{
divts = 1024*1024; tsunit = "MB";
}
//----------------------
if (drive.TotalFreeSpace < 1024)
{
divfs = 1; fsunit = "Byte(s)";
}
else if (drive.TotalFreeSpace < (1024 * 1024))
{
divfs = 1024; fsunit = "KB";
}
else if (drive.TotalFreeSpace < (1024 * 1024 * 1024))
{
divfs = 1024 * 1024; fsunit = "MB";
}
s = s +
" " + drive.VolumeLabel.ToString() +
"[" + drive.Name.Substring(0, 2) +
"]\t" + String.Format("{0,10:0.0}", ((fs/divfs)).ToString("N2")) + fsunit +
String.Format("{0,10:0.0}", (ts/divts).ToString("N2")) + tsunit +
"\t" + drive.DriveFormat.ToString() + "\t\t" + frprcntg.ToString("N2") + "%"+
"\t\t" + drive.DriveType.ToString();
s = s + "\n\r";
}
}
return s;
}
的輸出中應該是這樣的: -
只需提及:只需上傳圖片即可在圖片中添加圖片(點擊「插入圖片」圖標,這樣它就可以以內聯方式顯示,而不需要關注鏈接。 – Mic 2013-03-08 21:46:28
好像我太慢了...... – 2011-03-04 15:32:20
@Phil不在它多! – 2011-03-04 15:48:47