###如何用TreeView获取电脑磁盘下的路径
#region
private void Form1_Load(object sender, EventArgs e)
{
TreeNode rooteNode = new TreeNode("我的电脑");
treeView1.Nodes.Add(rooteNode);
}
private void TreeViewShow(TreeNode selectedNode)
{
try
{
if (selectedNode.Nodes.Count == 0)
{
if (selectedNode.Parent == null)
{
foreach (String drvName in Directory.GetLogicalDrives())
{
TreeNode drvNode = new TreeNode(drvName);
drvNode.Tag = drvName;
selectedNode.Nodes.Add(drvNode);
}
}
else
{
foreach (String dirName in Directory.GetDirectories((String)selectedNode.Tag))
{
TreeNode dirNode = new TreeNode(dirName);
dirNode.Tag = dirName;
selectedNode.Nodes.Add(dirName);
}
}
}
}
catch
{
return;
}
}
#endregion
string stap;
private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
{
TreeViewShow(e.Node);
stap = treeView1.SelectedNode.Text;
this.textBox1.Text = stap;
}
private void button1_Click(object sender, EventArgs e)
{
string path = stap;
if (path != null)
{
DirectoryInfo di = new DirectoryInfo(path);
//FileAttributes attr = File.GetAttributes(path);
if (di.Exists)
{
DialogResult resault = MessageBox.Show("是否删除", "提示!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (resault == DialogResult.OK)
{
Directory.Delete(path, true);
MessageBox.Show("删除成功", "提示!");
}
}
}
else
{
MessageBox.Show("路径不能为空", "提示");
}
}
}