轉(zhuǎn)帖|其它|編輯:郝浩|2011-07-08 17:51:18.000|閱讀 615 次
概述:本文主要介紹C#中各類獲取設(shè)備存儲信息的各類方法 ,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
普通WINFORM程序:
1.使用System.IO.DriveInfo來遍歷磁盤及其分區(qū)信息
引用System.IO后即可調(diào)用DriveInfo類來對磁盤空間信息進(jìn)行遍歷了,此外DriveInfo只有在普通WINFORM中可以調(diào)用,WINCE項目中未封裝此類。
//獲取磁盤設(shè)備
DriveInfo[] drives = DriveInfo.GetDrives();
//遍歷磁盤
foreach (DriveInfo drive in drives)
{
string drvInfo = "磁盤分區(qū)號:" + drive.Name + "盤" + "\t\n" +
"磁盤格式:" + drive.DriveFormat + "\t\n" +
"磁盤品牌:" + drive.DriveType + "\t\n" +
"磁盤卷標(biāo):" + drive.VolumeLabel + "\t\n" +
"磁盤總?cè)萘?quot; + drive.TotalSize + "\t\n" +
"磁盤空余容量:" + drive.TotalFreeSpace;
}
2.使用System.Management.ManagementClass來遍歷磁盤設(shè)備的各類屬性
與DriveInfo一樣,WINCE項目中未封裝此類。
ArrayList propNames = new ArrayList();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
//獲取本地磁盤各類屬性值
foreach (PropertyData driveProperty in props)
propNames.Add(driveProperty.Name);
int idx = 0;
ManagementObjectCollection drives = driveClass.GetInstances();
//遍歷該磁盤的各類屬性
foreach (ManagementObject drv in drives)
{
MessageBox.Show(string.Format(" 磁盤({0})的所有屬性 ", idx + 1));
foreach (string strProp in propNames)
MessageBox.Show(string.Format("屬性: {0}, 值: {1} ", strProp, drv[strProp]));
}
3.調(diào)用API函數(shù)GetVolumeInformation來獲取磁盤信息
定義API函數(shù)時,函數(shù)傳參前的"ref"亦可不加,但不加"ref"傳參不會有返回值。
[DllImport("Kernel32.dll")]
public static extern bool GetVolumeInformation(
ref string lpRootPathName, // 系統(tǒng)盤符根目錄或網(wǎng)絡(luò)服務(wù)器資源名
ref string lpVolumeNameBuffer, // 磁盤卷標(biāo)
ref int nVolumeNameSize, // 磁盤卷標(biāo)字符串的長度
ref int lpVolumeSerialNumber, // 磁盤卷標(biāo)的序列號
ref int lpMaximumComponentLength, // 文件名長度支持
ref int lpFileSystemFlags, // 文件系統(tǒng)標(biāo)記
ref string lpFileSystemNameBuffer, // 文件系統(tǒng)類型
ref int nFileSystemNameSize); // 文件系統(tǒng)類型字符串長度
調(diào)用
string strDriver = Path.GetFullPath(@"\");
bool res;
int serialNum = 0;
int size = 0;
string volume = "";
string vl= "";
string type = "";
string tl= "";
res = GetVolumeInformation(ref strDriver,ref volume,ref vl,
ref serialNum, ref size, 0,ref type,ref tl);
if (res)
{
MessageBox.Show("卷標(biāo): " + volume.Trim());
MessageBox.Show(string.Format("卷序列號: {0:X}", serialNum));
MessageBox.Show("文件名最大長度: " + size);
MessageBox.Show("磁盤分區(qū)系統(tǒng): " + type);
}
else
{
MessageBox.Show("該磁盤不存在");
}
WINCE程序:
1.調(diào)用API函數(shù)GetDiskFreeSpaceExW來獲取磁盤信息
在WINCE中"CoreDll.dll"相對應(yīng)普通XP中的"Kernel32.dll",其方法調(diào)用亦和GetVolumeInformation類似
//獲取磁盤信息本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園