程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> IO流的應用————小型資源管理器,io流資源管理器

IO流的應用————小型資源管理器,io流資源管理器

編輯:C#入門知識

IO流的應用————小型資源管理器,io流資源管理器


小型資源管理器

    

 

 

       

 

private void LoadTreeView()
{
  DirectoryInfo dir = new DirectoryInfo(@"E:\");
  DirectoryInfo[] dirs= dir.GetDirectories();
  foreach (DirectoryInfo item in dirs)
  {
    TreeNode tn = new TreeNode();
    tn.Text = item.Name;
    tn.Tag = item.FullName;
    this.treeView1.Nodes.Add(tn);
  }

}
private void Form1_Load(object sender, EventArgs e)
{
  LoadTreeView();

}
private void BindInfo(TreeNode node)
{
  DirectoryInfo dir = new DirectoryInfo(node.Tag.ToString());
  DirectoryInfo[] dirs = dir.GetDirectories();
  foreach (DirectoryInfo item in dirs)
  {
    TreeNode tn = new TreeNode();
    tn.Text = item.Name;
    tn.Tag = item.FullName;
    node.Nodes.Add(tn);
  }
  FileInfo[] fileInfo = dir.GetFiles();
  List<MyFile> files = new List<MyFile>();
  foreach (FileInfo item in fileInfo)
  {
    MyFile file = new MyFile();
    file.FileName = item.Name;
    float value = item.Length;
    file.FileLength = float.Parse(Math.Round(value / 1024, 2).ToString());
    file.FileType = item.Extension;
    file.FilePath = item.FullName;
    files.Add(file);
  }
  if (this.listView1.Items!=null)
  {
    this.listView1.Items.Clear();
  }
  foreach (MyFile item in files)
  {
    ListViewItem lvItem = new ListViewItem(item.FileName);
    lvItem.SubItems.Add(item.FileLength.ToString());
    lvItem.SubItems.Add(item.FileType);
    lvItem.SubItems.Add(item.FilePath);
    this.listView1.Items.Add(lvItem);
  }
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{

    TreeNode node = this.treeView1.SelectedNode;
    BindInfo(node);

}

private void 復制ToolStripMenuItem_Click(object sender, EventArgs e)
{
  DialogResult result= folderBrowserDialog1.ShowDialog();
  if (result==DialogResult.OK)
  {
    string sourcePath = this.listView1.SelectedItems[0].SubItems[3].Text;
    string desPath = folderBrowserDialog1.SelectedPath;
    desPath += "\\" + this.listView1.SelectedItems[0].SubItems[0].Text;
    File.Copy(sourcePath, desPath);
    MessageBox.Show("復制成功!!!");
  }
}

private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
{
    string sourcePath = this.listView1.SelectedItems[0].SubItems[3].Text;
    File.Delete(sourcePath);
    MessageBox.Show("刪除成功!!");
    TreeNode node = this.treeView1.SelectedNode;
    BindInfo(node);
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved