using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Day_小型资源
{
/// <summary>
/// 文件信息类
/// </summary>
public class MyFil
{
public float FileLength { get; set; }//文件长度,以KB为单位
public string FileName { get; set; }//文件名
public string FilePath { get; set; }//文件路径
public string FileType { get; set; }//文件类型
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Day_小型资源
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode node = this.treeView1.SelectedNode;
this.BindInfo(node);
}
private void BindInfo(TreeNode node)
{
DirectoryInfo directory = new DirectoryInfo(node.Tag.ToString());
DirectoryInfo[] dirs = directory.GetDirectories();
foreach (DirectoryInfo di in dirs)
{
TreeNode temp = new TreeNode();
temp.Text = di.Name;
temp.Tag = di.FullName;
node.Nodes.Add(temp);
}
FileInfo[] fileInfo = directory.GetFiles();
List<MyFil> files = new List<MyFil>();
foreach (FileInfo myFile in fileInfo)
{
MyFil file = new MyFil();
file.FileName = myFile.Name;
file.FileLength = myFile.Length;
file.FileType = myFile.Extension;
file.FilePath = myFile.FullName;
files.Add(file);
}
ListViewItem item = null;
this.listView1.Items.Clear();
foreach (MyFil file in files)
{
item = new ListViewItem();
item.Text = file.FileName;
item.SubItems.Add(file.FileLength.ToString());
item.SubItems.Add(file.FileType);
item.SubItems.Add(file.FilePath);
this.listView1.Items.Add(item);
}
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count == 0)
{
return;
}
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string sourcePath = listView1.SelectedItems[0].SubItems[3].Text;
string desPath = null;
if (result == DialogResult.OK)
{
desPath = fbd.SelectedPath;
desPath += "\\" + listView1.SelectedItems[0].SubItems[0].Text;
File.Copy(sourcePath, desPath);
MessageBox.Show("复制成功");
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count == 0)
{
return;
}
FolderBrowserDialog fbd = new FolderBrowserDialog();
string sourcePath = listView1.SelectedItems[0].SubItems[3].Text;
File.Delete(sourcePath);
MessageBox.Show("删除成功!");
//刷新一下
this.listView1.SelectedItems[0].Remove();
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-34740.html