获取本地计算机盘符
获取本地计算机第一个非光驱,软驱的盘符
using System.Management;
protected string GetDiskName()
{
string DiskName = string.Empty;
SelectQuery Disk = new SelectQuery( "Select * From Win32_LogicalDisk" );
ManagementObjectSearcher DiskSearch = new ManagementObjectSearcher( Disk );
StringBuilder sb = new StringBuilder();
foreach( ManagementBaseObject disk in DiskSearch.Get() )
{
// disk[ "DriveType" ]返回disk类型
//1 No type
//2 Floppy disk or USB disk
//3 Hard disk
//4 Removable drive or network drive
//5 CD-ROM
//6 RAM disk
if( disk[ "DriveType" ].ToString() == "3" )
{
sb.Append( disk[ "Name" ] );
sb.Append( "," );
}
}
string[] DiskList = sb.ToString().Substring( 0, sb.Length - 1 ).Split( new char[] { ',' } );
if( DiskList.Length > 0 )
{
DiskName = DiskList[ 0 ].ToString();
}
return DiskName;
}
protected string GetDiskName()
{
string DiskName = string.Empty;
SelectQuery Disk = new SelectQuery( "Select * From Win32_LogicalDisk" );
ManagementObjectSearcher DiskSearch = new ManagementObjectSearcher( Disk );
StringBuilder sb = new StringBuilder();
foreach( ManagementBaseObject disk in DiskSearch.Get() )
{
// disk[ "DriveType" ]返回disk类型
//1 No type
//2 Floppy disk or USB disk
//3 Hard disk
//4 Removable drive or network drive
//5 CD-ROM
//6 RAM disk
if( disk[ "DriveType" ].ToString() == "3" )
{
sb.Append( disk[ "Name" ] );
sb.Append( "," );
}
}
string[] DiskList = sb.ToString().Substring( 0, sb.Length - 1 ).Split( new char[] { ',' } );
if( DiskList.Length > 0 )
{
DiskName = DiskList[ 0 ].ToString();
}
return DiskName;
}