c# - 尝试读取 dvd/cd 驱动器并在 c# 中填充组合框
问题描述
我正在尝试用我所有的 dvd/cd 驱动器以 Windows 形式填充一个组合框,并说它们是否有光盘。
我正在尝试使用一个尝试发送对象和列表的类来做到这一点,但是该对象只是出现了错误,而列表只是出现了集合。
using System;
using System.Windows.Forms;
namespace DVD_main
{
public frmMain()
{
InitializeComponent();
cboDrive.Items.AddRange(Cls_DVD_Player.DVDDrvCtrls.cdDrv);
}
}
using System;
using System.Collections.Generic;
using System.Windows;
using System.IO;
using System.Runtime.InteropServices;
namespace Cls_DVD_Controls
{
class DVDDrvCtrls
{
private static readonly object cboDrive;
public static List<string> cdDrv()
{
List<string> drvNames = new List<string>();
DriveInfo[] cdDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in cdDrives)
{
if (d.DriveType == DriveType.CDRom && d.IsReady)
{
drvNames.Add(d.Name + " " + d.VolumeLabel);
MessageBox.Show(d.Name + " " + d.VolumeLabel);
}
else if (d.DriveType == DriveType.CDRom) {
drvNames.Add(d.Name + " Drive Empty");
}
}
//drvNames.AsReadOnly.cdDrives;
return drvNames;
}
}
}
解决方案
/*this was the best i could do couldn't find way for the class to pass the details i wanted this was the best i could find*/
private void CheckDrive()
{
//string[] dvdDrives = new Cls_DVD_Player.DVDDrvCtrls();
//List<string> drvNames = new List<string>();
DriveInfo[] cdDrives = DriveInfo.GetDrives();
cboDrive.Items.Clear();
foreach (DriveInfo d in cdDrives)
{
if (d.DriveType == DriveType.CDRom && d.IsReady)
{
cboDrive.Items.Add(d.Name + " " + d.VolumeLabel);
//MessageBox.Show(d.Name + " " + d.VolumeLabel);
}
else if (d.DriveType == DriveType.CDRom)
{
cboDrive.Items.Add(d.Name + " Drive Empty");
}
}
}
推荐阅读
- python - 使用文本文件中每一行的最后一个元素创建一个列表
- pine-script - 在特定情况下忽略的方法
- matlab - MATLAB 无法从串行 COM 端口读取数据
- python - 为什么我的 ROC 分数和 ROC 曲线显示不同的值?
- r - 在 freeBSD 中使用 Flang 编译 R 失败
- pandas - 如何在不读取所有数据的情况下获取此 CSV 的前 100 行
- reactjs - 如何使 React 函数推断打字稿类型?
- r - 保留基于先前行满足条件的数据行
- javascript - 使用 nodemailer、gmail(如果重要,可以使用 gcloud 主机)在生产环境中不会发送电子邮件。本地主机很好但是
- c++ - 重复符号错误 - 我在做我的包含吗?