c#实验

今天完成了一个c# 的实验,内容是一个资源管理器

 

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Security.AccessControl;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.listView1.View = View.Details;
this.treeView1.ImageList = this.imageList1;
string[] drives = Environment.GetLogicalDrives();

foreach (var drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Fixed)
{
//加载当前目录
this.treeView1.Nodes.Add(drive.Name, drive.Name);
foreach (var dir in Directory.GetDirectories(drive.Name))
{
TreeNode node = new TreeNode() { Tag = dir, Text = Path.GetFileName(dir) };
node.ImageIndex = 0;
node.SelectedImageIndex = 0;
node.Nodes.Add("");
//node = ListDirectories(node);
this.treeView1.Nodes[drive.Name].Nodes.Add(node);
}

//加载当前目录文件
string[] files = Directory.GetFiles(drive.Name);
foreach (var file in files)
{
TreeNode node = new TreeNode() { Tag = file, Text = Path.GetFileName(file) };
node.ImageIndex = 1;
node.SelectedImageIndex = 1;
this.treeView1.Nodes[drive.Name].Nodes.Add(node);
}
}
}
this.listView1.Columns.Add("文件名", 200);
this.listView1.Columns.Add("文件大小", 200);
this.listView1.Columns.Add("创建时间", 200);
this.listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);
this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);
}

/// <summary>
/// 递归遍历文件夹
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public TreeNode ListDirectories(TreeNode node)
{
foreach (var dir in Directory.GetDirectories(node.Tag.ToString(), "*.*", SearchOption.TopDirectoryOnly))
{
DirectoryInfo di = new DirectoryInfo(dir);
//过滤掉隐藏文件
if ((di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
TreeNode t = new TreeNode() { Tag = dir, Text = Path.GetFileName(dir) };
node.Nodes.Add(t);
//只要文件夹下面还有文件夹,就继续遍历当前节点下的路径
if (Directory.GetDirectories(t.Tag.ToString()).Length > 0)
{
ListDirectories(t);
}
}
}
return node;
}

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
//ListViewItem[] lvi = this.listView1.Items.OfType<ListViewItem>().OrderBy(x => new FileInfo(x.Tag.ToString())).ToArray();
//this.listView1.Items.AddRange(lvi);
this.listView1.ListViewItemSorter = new ListViewItemComparer(x++, this.listView1.Columns[e.Column].Text);
this.listView1.Sort();
}

void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
try
{
this.listView1.Items.Clear();
if (Directory.Exists(e.Node.Tag != null ? e.Node.Tag.ToString() : ""))
{
e.Node.Nodes.Clear();
foreach (var dir in Directory.GetDirectories(e.Node.Tag.ToString()))
{
TreeNode t = new TreeNode()
{
Tag = dir,
Text = Path.GetFileName(dir),
};
t.Nodes.Add("");
e.Node.Nodes.Add(t);
}

foreach (var file in Directory.GetFiles(e.Node.Tag.ToString(), "*.*", SearchOption.TopDirectoryOnly))
{
FileInfo fileInfo = new FileInfo(file);
ListViewItem item = new ListViewItem(new string[] {
fileInfo.Name,
(fileInfo.Length / 1024+1) + "kb",
fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
}) { Tag = fileInfo.FullName };
this.listView1.Items.Add(item);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
e.Node.Expand();
this.listView1.Items.Clear();
if (Directory.Exists(e.Node.Tag != null ? e.Node.Tag.ToString() : ""))
{
e.Node.Nodes.Clear();
foreach (var dir in Directory.GetDirectories(e.Node.Tag.ToString()))
{
TreeNode t = new TreeNode()
{
Tag = dir,
Text = Path.GetFileName(dir),
};
t.Nodes.Add("");
e.Node.Nodes.Add(t);
}

foreach (var file in Directory.GetFiles(e.Node.Tag.ToString(), "*.*", SearchOption.TopDirectoryOnly))
{
FileInfo fileInfo = new FileInfo(file);
ListViewItem item = new ListViewItem(new string[] {
fileInfo.Name,
(fileInfo.Length / 1024+1) + "kb",
fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
}) { Tag = fileInfo.FullName };
this.listView1.Items.Add(item);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void listView1_DoubleClick(object sender, EventArgs e)
{
//MessageBox.Show("hello");
toolStripMenuItem1.PerformClick();
}

//删除
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("您确定要删除当前文件吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr == DialogResult.OK)
{
string filePath = this.listView1.SelectedItems[0].Tag.ToString();
string fileName = this.listView1.SelectedItems[0].Text.ToString();
File.Delete(filePath);
this.listView1.Items.Remove(this.listView1.Items.OfType<ListViewItem>().FirstOrDefault(x => x.Text == fileName));
}
}

//打开
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
//MessageBox.Show(this.listView1.SelectedItems[0].Tag.ToString());
string filePath = this.listView1.SelectedItems[0].Tag.ToString();
//if (new FileInfo(filePath).Extension.Equals("exe"))
{
Process.Start(filePath);
}
}

//打开目录
private void 打开目录ToolStripMenuItem1_Click(object sender, EventArgs e)
{
string filePath = null;
string treeFilePath = this.treeView1.SelectedNode.Tag.ToString()
+ "\\" + this.treeView1.SelectedNode.Text.ToString();
if (this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0)
{
filePath = this.listView1.SelectedItems[0].Tag.ToString();
}
Process.Start(Path.GetDirectoryName(filePath ?? treeFilePath));
}

public void ListFiles(FileSystemInfo info)
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if (file != null && file.Name.Trim().LastIndexOf(".") > -1)
{
this.treeView1.Nodes.Add(file.FullName);
//this.listView1.Items.Add(file.FullName);
}
else
{
ListFiles(files[i]); //对于子目录,进行递归调用
}
}
}

int x = 0;//判断双击次数

/// <summary>
/// 比较器
/// </summary>
public class ListViewItemComparer : IComparer
{
private int _x;
private string _type;

public ListViewItemComparer(int x, string type)
{
this._x = x;
this._type = type;
}

public int Compare(object x, object y)
{
ListViewItem lv1 = (ListViewItem)x;
ListViewItem lv2 = (ListViewItem)y;

FileInfo file1 = new FileInfo(lv1.Tag.ToString());
FileInfo file2 = new FileInfo(lv2.Tag.ToString());

if (_type.Equals("文件名"))
{
if (_x % 2 == 0)
{
return file1.Name.CompareTo(file2.Name);
}
else
{
return file2.Name.CompareTo(file1.Name);
}
}
if (_type.Equals("文件大小"))
{
if (_x % 2 == 0)
{
return file1.Length.CompareTo(file2.Length);
}
else
{
return file2.Length.CompareTo(file1.Length);
}
}
if (_type.Equals("创建时间"))
{
if (_x % 2 == 0)
{
return file1.CreationTime.CompareTo(file2.CreationTime);
}
else
{
return file2.CreationTime.CompareTo(file1.CreationTime);
}
}
return 0;
}
}
}
}

 

posted @ 2021-12-07 18:59  Ryuuko-  阅读(39)  评论(0)    收藏  举报