c# - 检测光驱中的 cd/dvd 并在窗口媒体播放器上播放

标签 c# winforms

在我的窗体上,我在我的应用程序上创建了一个按钮。当我单击该按钮时,它将检查 CD 驱动器中是否有任何 CD/DVD。如果 CD 驱动器中有 CD/DVD,媒体播放器将在我已添加到应用程序的 Windows 媒体播放器上启动视频媒体。

到目前为止,我只能创建打开对话框以从 CD 中选择并播放它们。 谁能告诉我应该使用什么来检测驱动器中的 CD 介质?

EDIT://抱歉,我对 C# 不是很好,所以你们可以解释一下您提供的教程吗?

    private void Runbtn_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.mediaCollection.getByName("mediafile");


    }

    private void Stopbtn_Click(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.Ctlcontrols.stop();

    }
}

最佳答案

以下代码应该可以工作:(它在 C++ 中,使用 DllImport 调用 Win32 API - 解释 here)

char szDrives[MAX_PATH];      
long TotalNumberOfFreeBytes  = 0;
long FreeBytesAvailable = 0;

// Get all the drives on your system. Divide by 4 as strlen("C:\") ==  4
int noOfDrives =(GetLogicalDriveStrings(MAX_PATH,szDrives)/4);

for(int i=0;i<noOfDrives ;i++)
{
   // find CD ROM drives
   if (DRIVE_CDROM == GetDriveType(&drivestr[i*4]))
   {  
          if(!GetDiskFreeSpaceEx(&drivestr[i*4],
               &FreeBytesAvailable,
               NULL,
               &TotalNumberOfFreeBytes  ))
          {
             // Disk in drive, enumerate files 
             // using FindFirstFile/FindNextFile
             // and play video if any
          }
   }
}

GetDiskFreeSpaceEx 函数为所有 CD 请求的 TotalNumberOfFreeBytes 和 FreeBytesAvailable 返回零 (0),除非磁盘是 CD-RW 驱动器中未写入的 CD。

关于c# - 检测光驱中的 cd/dvd 并在窗口媒体播放器上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17013898/

相关文章:

c# - 需要有关 Regex 的帮助以从字符串中提取邮政编码

c# - 我如何知道接口(interface)何时直接在忽略继承接口(interface)的类型中实现?

c# - 将光标定位在 GridViewComboBoxColumn (Telerik) 中

c# - 无法在 list 资源中找到报告

wpf - 如何在 WPF 控件中运行 Google Earth

c# - 如何从服务器找出客户端的下载速度?

c# - 格式化 NodaTime 即时时包含尾随零?

c# - 从任务获取 HttpResponseMessage<HttpResponseMessage>

c# - 获取文本框行号

c# - 如何以编程方式为所有控件设置 ErrorProvider Icon