这是主界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 小型资源管理器
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 小型资源管理器
{
public class MyFile
{
public float FileLength { get; set; }//文件长度
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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace 小型资源管理器
{
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 directoryInfo = new DirectoryInfo(node.Tag.ToString());
DirectoryInfo[] dirs = directoryInfo.GetDirectories();
foreach (DirectoryInfo di in dirs)
{
TreeNode temp = new TreeNode();
temp.Text = di.Name;
temp.Tag = di.FullName;
node.Nodes.Add(temp);
}
//获取目录下的文件列表
FileInfo[] fileInfo = directoryInfo.GetFiles();
//定义泛型集合存储文件信息
List<MyFile> files = new List<MyFile>();
//遍历文件列表
foreach (FileInfo myFile in fileInfo)
{
MyFile file = new MyFile();
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 (MyFile 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 Form1_Load(object sender, EventArgs e)
{
TreeNode tn = new TreeNode("E:\\");
tn.Tag = "E://";
treeView1.Nodes.Add(tn);
}
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;
//ListView1 表示显示文件信息的ListView对象
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();
}
}
}