[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool GetDiskFreeSpace(
[MarshalAs(UnmanagedType.LPTStr)]
string rootPathName,
ref int sectorsPerCluster,
ref int bytesPerSector,
ref int numberOfFreeClusters,
ref int totalNumberOfClusters);
可以得到驱动器空闲大小
static extern bool GetDiskFreeSpace(
[MarshalAs(UnmanagedType.LPTStr)]
string rootPathName,
ref int sectorsPerCluster,
ref int bytesPerSector,
ref int numberOfFreeClusters,
ref int totalNumberOfClusters);
可以得到驱动器空闲大小
LPCTSTR lpDirectoryName, // 驱动器名
PULARGE_INTEGER lpFreeBytesAvailable, // 可引用大小
PULARGE_INTEGER lpTotalNumberOfBytes, // 总大小
PULARGE_INTEGER lpTotalNumberOfFreeBytes // 空闲大小
);
用这个吧,上一个只能用于小于2G的盘,仿照上面的加入引用文件和更改一下参数类型就可以了
private void disk(string _strIP,string _strUsername,string _strPassword)
{
string m_strIP = _strIP;
string m_strUsername = _strUsername;
string m_strPassword = _strPassword; const int Movable=2;
const int LocalDisk=3;
const int CD=5;
string type="";
ConnectionOptions co = new ConnectionOptions();
co.Username = m_strUsername;
co.Password = m_strPassword;
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + m_strIP + "\\root\\cimv2", co);
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("select * from win32_logicaldisk");
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection=query.Get();
foreach (ManagementObject mo in queryCollection)
{
switch (int.Parse(mo["DriveType"].ToString()))
{
case Movable:
type="移动设备";
break;
case LocalDisk:
type="本地磁盘";
ListViewItem item = new ListViewItem();
item.SubItems.Add( type + mo["Name"].ToString());
string m_strSize = mo["FreeSpace"].ToString();
string temp = string.Empty;
long size = long.Parse( m_strSize );
float f = 0.0f;
if( size < 1024 * 1024 )
{
size = size / (1024 * 1024 );
temp = size + "M";
}
else
{
f = size / (1024 * 1024 * 1024) ;
temp = f + "G";
}
item.SubItems.Add( temp );
break;
case CD:
type="CD驱动器";
break;
default:
break;
}
}
}
PropertyDataCollection diskProperties = disk.Properties;
ListBox l = new ListBox();
l.Location = new Point(30, 50);
l.Size = new Size(400, 300);
this.Controls.Add(l);
foreach (PropertyData diskProperty in diskProperties)
{
l.Items.Add(diskProperty.Name + " = " + diskProperty.Value);
}