怎样才能遍历磁盘的所有逻辑分区ne?????
请各位大侠指教,最好给点代码提示。

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.IO;namespace WindowsApp.FirstApp
    {
    public class Form18 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.PrintDialog printDialog1;
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.Windows.Forms.MenuItem menuItem2;
    private System.Windows.Forms.MenuItem menuItem3;
    private System.Windows.Forms.MenuItem menuItem4;
    private System.Windows.Forms.MenuItem menuItem5;
    private System.Windows.Forms.MenuItem menuItem6;
    private System.Windows.Forms.MenuItem menuItem7;
    private System.Windows.Forms.MenuItem menuItem8;
    private System.Windows.Forms.MenuItem menuItem9;
    private System.Windows.Forms.MenuItem menuItem10;
    private System.Windows.Forms.MenuItem menuItem11;
    private System.Windows.Forms.MenuItem menuItem12;
    private System.Windows.Forms.MenuItem menuItem13;
    private System.Windows.Forms.MenuItem menuItem14;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.ListBox listBox2;
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.TextBox textBox1;
    private OpenFileDialog fpath;
    private System.IO.DirectoryInfo directoryInfo;
    private System.Diagnostics.ProcessStartInfo proStartInfo;
    private System.Diagnostics.Process process;
    private System.IO.FileInfo fileInfo;
    private int pasteFileFlag=0;
    private int pasteDirectoryFlag=0;
    private string copyFileFullName;
    private string copyFileName;
    private string copyDirectoryFullName;
    private string copyDirectoryName; public Form18()
    {
    InitializeComponent();
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.printDialog1 = new System.Windows.Forms.PrintDialog();
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.menuItem3 = new System.Windows.Forms.MenuItem();
    this.menuItem4 = new System.Windows.Forms.MenuItem();
    this.menuItem2 = new System.Windows.Forms.MenuItem();
    this.menuItem5 = new System.Windows.Forms.MenuItem();
    this.menuItem6 = new System.Windows.Forms.MenuItem();
    this.menuItem7 = new System.Windows.Forms.MenuItem();
    this.menuItem8 = new System.Windows.Forms.MenuItem();
    this.menuItem9 = new System.Windows.Forms.MenuItem();
    this.menuItem11 = new System.Windows.Forms.MenuItem();
    this.menuItem12 = new System.Windows.Forms.MenuItem();
    this.menuItem10 = new System.Windows.Forms.MenuItem();
    this.menuItem13 = new System.Windows.Forms.MenuItem();
    this.menuItem14 = new System.Windows.Forms.MenuItem();
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.listBox2 = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    // 
    // mainMenu1
    // 
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem1,
      this.menuItem2,
      this.menuItem7,
      this.menuItem9,
      this.menuItem10});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem3,
      this.menuItem4});
    this.menuItem1.Text = "添加(&A)";
    // 
    // menuItem3
    // 
    this.menuItem3.Index = 0;
    this.menuItem3.Text = "添加目录";
    // 
    // menuItem4
    // 
    this.menuItem4.Index = 1;
    this.menuItem4.Text = "添加文件";
    // 
    // menuItem2
    // 
    this.menuItem2.Index = 1;
    this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem5,
      this.menuItem6});
    this.menuItem2.Text = "删除(&D)";
    // 
    // menuItem5
    // 
    this.menuItem5.Index = 0;
    this.menuItem5.Text = "删除目录";
    this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
    // 
    // menuItem6
    // 
    this.menuItem6.Index = 1;
    this.menuItem6.Text = "删除文件";
    this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
    // 
    // menuItem7
      

  2.   

    this.menuItem7.Index = 2;
    this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem8});
    this.menuItem7.Text = "复制&C)";
    // 
    // menuItem8
    // 
    this.menuItem8.Index = 0;
    this.menuItem8.Text = "复制文件";
    this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
    // 
    // menuItem9
    // 
    this.menuItem9.Index = 3;
    this.menuItem9.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
      this.menuItem11,
      this.menuItem12});
    this.menuItem9.Text = "剪切(X)";
    // 
    // menuItem11
    // 
    this.menuItem11.Index = 0;
    this.menuItem11.Text = "剪切文件";
    this.menuItem11.Click += new System.EventHandler(this.menuItem11_Click);
    // 
    // menuItem12
    // 
    this.menuItem12.Index = 1;
    this.menuItem12.Text = "剪切目录";
    this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
    // 
    // menuItem10
    // 
    this.menuItem10.Index = 4;
    this.menuItem10.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
       this.menuItem13,
       this.menuItem14});
    this.menuItem10.Text = "粘贴(&Y)";
    // 
    // menuItem13
    // 
    this.menuItem13.Index = 0;
    this.menuItem13.Text = "粘贴文件";
    this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
    // 
    // menuItem14
    // 
    this.menuItem14.Index = 1;
    this.menuItem14.Text = "粘贴目录";
    this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(0, 1);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "链接地址:";
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
    // 
    // textBox1
    // 
    this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.textBox1.Location = new System.Drawing.Point(64, 1);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(200, 21);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "f:\\";
    // 
    // button1
    // 
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
    this.button1.Location = new System.Drawing.Point(264, 1);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(48, 23);
    this.button1.TabIndex = 2;
    this.button1.Text = "转到";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // listBox1
    // 
    this.listBox1.HorizontalScrollbar = true;
    this.listBox1.ItemHeight = 12;
    this.listBox1.Location = new System.Drawing.Point(0, 24);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(104, 232);
    this.listBox1.TabIndex = 3;
    this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
    // 
    // listBox2
    // 
    this.listBox2.HorizontalScrollbar = true;
    this.listBox2.ItemHeight = 12;
    this.listBox2.Location = new System.Drawing.Point(104, 24);
    this.listBox2.Name = "listBox2";
    this.listBox2.Size = new System.Drawing.Size(296, 232);
    this.listBox2.TabIndex = 4;
    this.listBox2.DoubleClick += new System.EventHandler(this.listBox2_DoubleClick);
    // 
    // Form18
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(310, 205);
    this.Controls.Add(this.listBox2);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
    this.Menu = this.mainMenu1;
    this.Name = "Form18";
    this.Text = "资源管理器";
    this.ResumeLayout(false); }
    #endregion
      

  3.   

    private void shuaxin()
    {
    try
    {
    directoryInfo=new DirectoryInfo(textBox1.Text);
    this.listBox1.Items.Clear();
    this.listBox2.Items.Clear();
    this.listBox1.Items.Add("..");

    foreach(DirectoryInfo dir in directoryInfo.GetDirectories())
    {
    this.listBox1.Items.Add(dir);
    }
    foreach(FileInfo f in directoryInfo.GetFiles("*.*"))
    {
    this.listBox2.Items.Add(f);
    }
    }
    catch
    {
    MessageBox.Show("系统不能找到路径!","错误");
    }
    } private void listBox1_DoubleClick(object sender, System.EventArgs e)
    {
    try
    {
    if(this.listBox1.SelectedItem.ToString()=="..")
    {
    try
    {
    directoryInfo=new System.IO.DirectoryInfo(this.textBox1.Text);
    this.listBox1.Items.Clear();
    this.listBox2.Items.Clear();
    this.listBox1.Items.Add("..");
    foreach(DirectoryInfo dir in directoryInfo.Parent.GetDirectories())
    {
    this.listBox1.Items.Add(dir);
    }
    foreach(FileInfo f in directoryInfo.Parent.GetFiles("*.*"))
    {
    this.listBox2.Items.Add(f);
    }
    textBox1.Text=directoryInfo.Parent.FullName.ToString()+"\\";
    }
    catch
    {
    this.listBox1.Items.Clear();
    this.listBox2.Items.Clear();
    foreach(string str in Directory.GetLogicalDrives())
    {
    this.listBox1.Items.Add(str);
    }
    this.textBox1.Text="";
    }
    }
    else
    {
    textBox1.Text+=this.listBox1.SelectedItem+"\\";
    shuaxin();
    }
    }
    catch
    {
    }
    } private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    shuaxin();
    }
    catch
    {
    MessageBox.Show("系统不能找到路径!","错误");
    }
    } private void listBox2_DoubleClick(object sender, System.EventArgs e)
    {
    try
    {
    try
    {
    System.Diagnostics.Process.Start(this.textBox1.Text+this.listBox2.SelectedItem);
    }
    catch
    {
    if(this.listBox2.SelectedItem.ToString()!="")
    {
    fpath=new OpenFileDialog();
    fpath.InitialDirectory="c:\\";
    fpath.Filter="选择打开文件的可执行文件|*.exe;*.com";
    fpath.RestoreDirectory=true;
    fpath.Title="选择打开文件的可执行文件";
    proStartInfo=new System.Diagnostics.ProcessStartInfo();
    if(fpath.ShowDialog()==DialogResult.OK)
    {
    proStartInfo.FileName=fpath.FileName;
    proStartInfo.Arguments=this.textBox1+this.listBox2.SelectedItem.ToString();
    process=System.Diagnostics.Process.Start(proStartInfo);
    }
    }
    }
    }
    catch
    {
    }
    } private void menuItem5_Click(object sender, System.EventArgs e)
    {
    try
    {
    directoryInfo=new DirectoryInfo(this.textBox1.Text+this.listBox1.SelectedItem);
    directoryInfo.Delete(true);
    shuaxin();
    }
    catch
    {
    MessageBox.Show("请选择删除对象!","错误");
    }
    } private void menuItem6_Click(object sender, System.EventArgs e)
    {
    try
    {
    fileInfo=new FileInfo(this.textBox1.Text+this.listBox2.SelectedItem);
    fileInfo.Delete();
    shuaxin();
    }
    catch
    {
    MessageBox.Show("请选择删除对象!","错误");
    }
    } private void menuItem8_Click(object sender, System.EventArgs e)
    {
    try
    {
    copyFileFullName=this.textBox1.Text+this.listBox2.SelectedItem.ToString();
    copyFileName=this.listBox2.SelectedItem.ToString();
    pasteFileFlag=1;
    }
    catch
    {
    MessageBox.Show("请选择复制对象!","错误");
    }
    } private void menuItem13_Click(object sender, System.EventArgs e)
    {
    if(pasteFileFlag==1)
    {
    fileInfo=new FileInfo(copyFileFullName);
    fileInfo.CopyTo(this.textBox1.Text+copyFileName);
    }
    else if(pasteFileFlag==2)
    {
    fileInfo=new FileInfo(copyFileFullName);
    fileInfo.MoveTo(this.textBox1.Text+copyFileName);
    }
    shuaxin();
    } private void menuItem11_Click(object sender, System.EventArgs e)
    {
    try
    {
    copyFileFullName=this.textBox1.Text+this.listBox2.SelectedItem.ToString();
    copyFileName=this.listBox2.SelectedItem.ToString();
    pasteFileFlag=2;
    }
    catch
    {
    MessageBox.Show("请选择剪切对象!","错误");
    }
    } private void menuItem12_Click(object sender, System.EventArgs e)
    {
    try
    {
    copyDirectoryFullName=this.textBox1.Text+this.listBox1.SelectedItem.ToString();
    copyDirectoryName=this.listBox1.SelectedItem.ToString();
    pasteDirectoryFlag=1;
    }
    catch
    {
    MessageBox.Show("请选择剪切对象!","错误");
    }
    } private void menuItem14_Click(object sender, System.EventArgs e)
    {
    if(pasteDirectoryFlag==1)
    {
    directoryInfo=new DirectoryInfo(copyDirectoryFullName);
    directoryInfo.MoveTo(this.textBox1.Text+copyDirectoryName);
    shuaxin();
    }
    } }
    }
      

  4.   

    to zhaoliang_chen(龙行天下)
    大哥辛苦了,你给的好像和我要的有点区别。
    还有你那个程序,每入口点运行不起赛!
    thanks all the same!
      

  5.   

    最简单的办法 
    TreeNode node = new TreeNode("我的电脑");
    treeView.Nodes.Add(node);    //加入一个我的电脑节点string[] drivesName = System.IO.Directory.GetLogicalDrives()  //取得驱动器列表的集合
    foreach(string name in drivesName)  //用foreach遍历集合
    {
      TreeNode drivesNode = new TreeNode(name);
      node.Nodes.Add(drivesNode);     //加到我的电脑节点下
    }
      

  6.   

    东哥,分给我三,我给你找到了哈,自己看^_^
    http://www.ddvip.net/program/c-/index3/10.htm
    http://developer.ccidnet.com/pub/disp/Article?columnID=322&articleID=10962&pageNO=1
    这2个地方都有.