C#检测光驱里的光盘是否已经加载

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

登录
注册
订阅RSS
网站地图
脚本分享网
using System;
using System.Management;
  
class App
{
  public static void Main()
  {
    SelectQuery query = new SelectQuery( "select * from win32_logicaldisk where drivetype=5" );
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
  
    foreach( ManagementObject mo in searcher.Get() )
    {
      // If both properties are null I suppose there's no CD
      if( ( mo["volumename"] != null ) || ( mo["volumeserialnumber"] != null ) )
      {
        Console.WriteLine( "CD is named: {0}", mo["volumename"] );
        Console.WriteLine( "CD Serial Number: {0}", mo["volumeserialnumber"] );
      }
      else
      {
        Console.WriteLine( "No CD in Unit" );
      }
    }
  
    // Here to stop app from closing
    Console.WriteLine( "\nPress Return to exit." );
    Console.Read();
  }
}