c# 怎么实现安全弹出U盘

EasonLouis 2015-12-31 04:57:51
我有一个选择TreeView控件上TreeNode的U盘点击弹出的代码,但我想把U盘信息放在comboBox控件中选择,不知道怎么弄。
       private void LoadItems()
{
_loading = true;
treeViewDisks.Nodes.Clear();
comboBox1.Items.Clear();

TreeNode root = treeViewDisks.Nodes.Add("Computer");
root.ImageIndex = (int)IconIndex.MyComputer;
root.SelectedImageIndex = root.ImageIndex;

// display volumes
VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();
TreeNode volumesNode = new TreeNode("Volumes");
volumesNode.ImageIndex = (int)IconIndex.Volume;
volumesNode.SelectedImageIndex = volumesNode.ImageIndex;
root.Nodes.Add(volumesNode);

foreach (Volume device in volumeDeviceClass.Devices)
{
if ((usbOnlyToolStripMenuItem.Checked) && (!device.IsUsb))
continue;

string text = null;
if ((device.LogicalDrive != null) && (device.LogicalDrive.Length > 0))
{
text += device.LogicalDrive;
}

if (text != null)
{
text += " ";
}
text += device.Description;
if (device.FriendlyName != null)
{
if (text != null)
{
text += " - ";
}
text += device.FriendlyName;
}

TreeNode deviceNode = volumesNode.Nodes.Add(text);

if (device.IsUsb)
{
deviceNode.ImageIndex = (int)IconIndex.Box;
deviceNode.SelectedImageIndex = deviceNode.ImageIndex;
}
deviceNode.Tag = device;

foreach (Device disk in device.Disks)
{
TreeNode diskNode = deviceNode.Nodes.Add(disk.Description + " - " + disk.FriendlyName);
diskNode.ImageIndex = deviceNode.ImageIndex;
diskNode.SelectedImageIndex = diskNode.ImageIndex;
diskNode.Tag = device;
}
}

root.ExpandAll();
_loading = false;
}

private void treeViewDisks_AfterSelect(object sender, TreeViewEventArgs e)
{
// update property grid
Device device = (Device)e.Node.Tag;
if (device == null)
{
propertyGridDevice.SelectedObject = null;
return;
}

propertyGridDevice.SelectedObject = device;
}
private void usbOnlyToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadItems();
}
private void ejectToolStripMenuItem_Click(object sender, EventArgs e)
{
Device device = GetSelectedDevice();
if (device == null)
return;

string s = device.Eject(true);
if (s != null)
{
MessageBox.Show(this, s, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void treeViewDisks_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
treeViewDisks.SelectedNode = e.Node;
}

private Device GetSelectedDevice()
{
TreeNode node = treeViewDisks.SelectedNode;
if (node == null)
{
return null;
}

return (Device)node.Tag;
}
...全文
381 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
EasonLouis 2016-01-08
  • 打赏
  • 举报
回复
没人回答,在代码中添加comboBox1.Tag = device;
EasonLouis 2015-12-31
  • 打赏
  • 举报
回复
UP 求大神给答疑解惑
EasonLouis 2015-12-31
  • 打赏
  • 举报
回复
完整代码链接:http://download.csdn.net/detail/nicolaus1/9385821

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧