using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dispaly_all_logic_driver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//把裝置代碼加入ListBox裡
List<string> list = get_all_logic_driver();
foreach (string drive in list)
listBox1.Items.Add(drive);
}
/// <summary>
/// 取得電腦上所有邏輯裝置代碼
/// </summary>
/// <returns></returns>
private List<string> get_all_logic_driver()
{
List<string> list = new List<string>();
foreach (string drive in Environment.GetLogicalDrives())
{
list.Add(drive);
}
return list;
}
}
}
執行畫面如下
如果要識別出那個是光碟機需要呼叫WINDOWS API 來判斷DviveType
[DllImport("kernel32", SetLastError = true)]
public static extern int GetDriveType(string driveLetter);
完整程式如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dispaly_all_logic_driver
{
public partial class Form1 : Form
{
[DllImport("kernel32", SetLastError = true)]
public static extern int GetDriveType(string driveLetter);
public Form1()
{
InitializeComponent();
//把裝置代碼加入ListBox裡
List<string> list = get_all_logic_driver();
foreach (string drive in list)
listBox1.Items.Add(drive);
}
/// <summary>
/// 取得電腦上所有邏輯裝置代碼
/// </summary>
/// <returns></returns>
private List<string> get_all_logic_driver()
{
List<string> list = new List<string>();
foreach (string drive in Environment.GetLogicalDrives())
{
if (GetDriveType(drive) == 5) //CDROM type
list.Add(drive);
}
return list;
}
}
}
執行結果
我的裝置
type代碼
Value | Constant | Meaning |
---|---|---|
1 | SYSTEM_WINDRIVE_ERROR | Non-existent drive or other error |
2 | SYSTEM_WINDRIVE_REMOVABLE | Removable drive (e.g. floppy) |
3 | SYSTEM_WINDRIVE_FIXED | Harddisk |
4 | SYSTEM_WINDRIVE_REMOTE | Network share |
5 | SYSTEM_WINDRIVE_CDROM | CD-Rom or DVD |
6 | SYSTEM_WINDRIVE_RAMDISK | RAM disk |
參考資料請參考這一篇
https://pear.php.net/manual/fr/package.system.windrives.windrives.getdrivetype.php
沒有留言:
張貼留言