如果我单击硬盘名称,我可以在文本框中看到驱动器类型。这工作正常。但是,如果我在网络接口上尝试此代码,它不起作用。它给出了错误 CS0144:无法创建抽象类型或接口“NetworkInterface”的实例
这是代码:
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
textBox1.Text = "";
DriveInfo[] Drives = DriveInfo.GetDrives();
foreach (DriveInfo drv in Drives)
{
listBox1.Items.Add(drv.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
String strDrive = (string)listBox1.SelectedItem;
DriveInfo drive = new DriveInfo(strDrive);
textBox1.Text = drive.DriveType.ToString();
}
Run Code Online (Sandbox Code Playgroud)
这里是不工作的网络接口:
private void button3_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
textBox2.Text = "";
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
listBox2.Items.Add(adapter.Name);
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{ …Run Code Online (Sandbox Code Playgroud)