小型資源管理器
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);
}