C#资源(文件)管理器 实验报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验5 文件管理器
【实验目的】
⏹进一步实践windows窗口程序开发的流程;
⏹掌握并熟练使用后台代码添加控件并定义布局和格式以及交互的技能。

【实验环境】
Visual Studio 2005(或更高版本)
【实验内容】
设计一个基于后台代码添加控件的方式来实现文件管理器的显示和交互功能的Winform程序,鼓励扩展其他功能。

基本功能如下图:
【实验记录】
文件菜单:
新建文件夹:提供即时重命名
新建文件夹遇到相同明添加序号
新建文本文档
新建文本文档遇到相同明添加序号
选中单个项目激活重命名和删除
选中多个项目禁用重命名
编辑菜单:(撤销功能未完成)
选择项目激活剪切和复制状态栏显示选择项信息
点击复制或剪切激活复制
在同目录下黏贴同名添加“- 副本”
点击全选
查看菜单:
点击状态栏默认打开点击后关闭
点击大图标:
点击小图标
点击详细信息
排序方式类型点击列头同样实现
排序方式递减
转至:前进禁用
点击后退前进启用
点击前进
点击向上一级:
向上到磁盘跟目录禁用向上一级
帮助关于:
工具栏与菜单栏选定状态同步功能相同
右键菜单:启用状态与菜单栏工具栏同步功能相同
本软件提供单项(多项未完成请勿测试)拖拽移动项目无法截图展示请自行测试【核心代码】
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.AccessControl;
using Microsoft.Win32;
using System.IO;
using System.Collections;
namespace FileExplorer
{
public partial class Form1 : Form
{
static DirectoryInfo currentDir;
string[] newpath = new string[1024];
string[] oldpath = new string[1024];
string[] type = new string[1024];
List<string> ls = new List<string>();
int ind;
ListViewItemSorter sorter;
public Form1()
{
InitializeComponent();
sorter = new ListViewItemSorter();
listView1.ListViewItemSorter = sorter;
sorter.SortColumn = 0;
sorter.SortOrder = SortOrder.Ascending;
}
private void Form1_Load(object sender, EventArgs e)
{
getDisks();
toolStripStatusLabel1.Text = DateTime.Now.ToString();
}
private void getDisks()
{
foreach (DriveInfo d in DriveInfo.GetDrives())
{
if (d.IsReady)
{
TreeNode root = new TreeNode();
root.Tag = ;
treeView1.Nodes.Add(root);
getAll(root);
}
}
}
private void getAll(TreeNode parentNode)
{
#region
DirectoryInfo parentDir = new DirectoryInfo(parentNode.FullPath);
DirectorySecurity s = new DirectorySecurity(parentNode.FullPath, AccessControlSections.Access);
if (!s.AreAccessRulesProtected)
{
if (parentDir.GetDirectories() != null)
{
foreach (DirectoryInfo childDir in parentDir.GetDirectories())
{
TreeNode childNode = new TreeNode();
childNode.Tag = childDir.FullName;
parentNode.Nodes.Add(childNode);
// getAll(childNode);
}
}
}
#endregion
// #region
// DirectoryInfo directory;
// try
// {
// // 第一层路径
// if (parentNode.Nodes.Count == 0)
// {
// directory = new DirectoryInfo(parentNode.FullPath);
// foreach (DirectoryInfo dir in directory.GetDirectories())
// {
// TreeNode newNode = new TreeNode();
// parentNode.Nodes.Add(newNode);
// }
// }
// // 第二层路径
// foreach (TreeNode node in parentNode.Nodes)
// {
// if (node.Nodes.Count == 0)
// {
// directory = new DirectoryInfo(node.FullPath);
// foreach (DirectoryInfo dir in directory.GetDirectories())
// {
// TreeNode newNode = new TreeNode();
// node.Nodes.Add(newNode);
// }
// }
// }
// }
// catch (Exception doh)
// {
// Console.WriteLine(doh.Message);
// }
//#endregion
}
private void showListView(string path)
{
this.listView1.Items.Clear();
currentDir = new DirectoryInfo(path);
DirectorySecurity s = new DirectorySecurity(path, AccessControlSections.Access);
if (!s.AreAccessRulesProtected)
{
if (currentDir.GetDirectories() != null)
{
foreach (DirectoryInfo dir in currentDir.GetDirectories())
{
ListViewItem item = new ListViewItem();
//
this.imageList2.Images.Add(System.Drawing.Icon.FromHandle(dir.FullName));
//FileAttributes
item.SubItems.Add("");
item.SubItems.Add("文件夹");
item.SubItems.Add(File.GetCreationTime(dir.FullName).ToString("u"));
item.SubItems.Add(File.GetLastWriteTime(dir.FullName).ToString("u"));
item.SubItems.Add(File.GetLastAccessTime(dir.FullName).ToString("u"));
item.Tag = "0";
item.ToolTipText = dir.FullName;
item.ImageIndex = 0;
//item.ImageIndex = this.imageList2.Images.Count - 1;
this.listView1.Items.Add(item);
}
foreach (FileInfo file in currentDir.GetFiles())
{
ListViewItem item = new ListViewItem();
item.SubItems.Add(file.Length<1024*1024?""+file.Length/1024+"KB":""+file.Length/1024/1024+" MB");
item.SubItems.Add(checkFileType(file.FullName));
item.SubItems.Add(File.GetCreationTime(file.FullName).ToString("u"));
item.SubItems.Add(File.GetLastWriteTime(file.FullName).ToString("u"));
item.SubItems.Add(File.GetLastAccessTime(file.FullName).ToString("u"));
//if
(!this.imageList1.Images.ContainsKey(Path.GetExtension()))
//{
// this.imageList1.Images.Add(Path.GetExtension(), System.Drawing.Icon.ExtractAssociatedIcon(file.FullName));
//}
this.imageList2.Images.Add(System.Drawing.Icon.ExtractAssociatedIcon(file.FullName));
this.imageList3.Images.Add(System.Drawing.Icon.ExtractAssociatedIcon(file.FullName));
this.imageList4.Images.Add(System.Drawing.Icon.ExtractAssociatedIcon(file.FullName));
this.imageList5.Images.Add(System.Drawing.Icon.ExtractAssociatedIcon(file.FullName));
item.Tag = "1";
item.ToolTipText = file.FullName;
if(this.listView1.View==rgeIcon)
item.ImageIndex = this.imageList2.Images.Count - 1;
if (this.listView1.View == rgeIcon)
item.ImageIndex = this.imageList2.Images.Count - 1;
if (this.listView1.View == View.SmallIcon)
item.ImageIndex = this.imageList3.Images.Count - 1;
if (this.listView1.View == View.List)
item.ImageIndex = this.imageList4.Images.Count - 1;
if (this.listView1.View == View.Details)
item.ImageIndex = this.imageList5.Images.Count - 1;
// item.ImageIndex = this.imageList1.Images.IndexOfKey(Path.GetExtension());;
this.listView1.Items.Add(item);
}
}
}
toolStripStatusLabel2.Text ="\n"+listView1.Items.Count + "个项目";
}
private string checkFileType(string path)
{
string ext = Path.GetExtension(path);
try
{
string desc = (string)Registry.ClassesRoot.OpenSubKey(ext).GetValue(null);
return (string)Registry.ClassesRoot.OpenSubKey(desc).GetValue(null);
}
catch (NullReferenceException nre) {
return "不可识别的文件类型";
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
getAll(e.Node); // 取得选择节点的子文件夹
addressTB.Text = e.Node.FullPath; // 更新文本框内容
currentDir = new DirectoryInfo(e.Node.FullPath);
showListView(e.Node.FullPath);
if (currentDir.Parent != null)
{
toolStripButton1.Enabled = true;
向上一级ToolStripMenuItem.Enabled = true;
}
ls.Add(addressTB.Text);
ind = ls.Count - 1;
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
foreach (ListViewItem item in this.listView1.SelectedItems)
{
if (item.Tag.ToString() == "0")
{
showListView(item.ToolTipText);
addressTB.Text = item.ToolTipText;
ls.Add(addressTB.Text);
ind = ls.Count-1;
}
if (item.Tag.ToString() == "1")
{
try { System.Diagnostics.Process.Start(item.ToolTipText); }
catch (Win32Exception w32e)
{
MessageBox.Show("亲,对不起,系统没能找到合适的程序打开该文件哦!");
}
}
}
if (currentDir.Parent != null)
{
toolStripButton1.Enabled = true;
向上一级ToolStripMenuItem.Enabled = true;
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
后退ToolStripMenuItem_Click(sender, e);
}
private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)
{
viewcheck();
大图标CToolStripMenuItem.Checked = true;
大图标ToolStripMenuItem.Checked = true;
this.listView1.View = rgeIcon;
}
private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)
{
viewcheck();
小图标OToolStripMenuItem.Checked = true;
小图标ToolStripMenuItem.Checked = true;
this.listView1.View = View.SmallIcon;
}
private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
viewcheck();
列表ToolStripMenuItem1.Checked = true;
列表ToolStripMenuItem.Checked = true;
this.listView1.View = View.List;
}
private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
viewcheck();
详细信息ToolStripMenuItem.Checked = true;
详细信息ToolStripMenuItem1.Checked = true;
this.listView1.View = View.Details;
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
this.treeView1.Width = this.Width * 24 / 100;
this.listView1.Width = this.Width -this.treeView1.Width-10;
}
private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)//重命名后判断是否存在同名
{
string temp = listView1.SelectedItems[0].Text;
e.CancelEdit = false;
if (bel == null)//取消重命名则退出编辑
e.CancelEdit = true;
else
{
if (Directory.Exists(currentDir.FullName + "\\" + bel))
{
MessageBox.Show("此目标已包含名为\"" + bel + "\"", "重命名失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.CancelEdit = true;//同名则自动取消重命名恢复原有命名
}
else
{
Directory.Move(currentDir.FullName + "\\" + temp, currentDir.FullName + "\\" + bel);
}
}
}
private void 文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = currentDir.FullName + "\\新建文件夹";
path = directorynum(path);
Directory.CreateDirectory(path);
ListViewItem item = new ListViewItem(path.Substring(stIndexOf('\\') + 1));//添加listview的项
item.SubItems.Add("");
item.SubItems.Add("文件夹");
item.SubItems.Add(File.GetCreationTime(path).ToString("u"));
item.SubItems.Add(File.GetLastWriteTime(path).ToString("u"));
item.SubItems.Add(File.GetLastAccessTime(path).ToString("u"));
item.Tag = "0";
item.ToolTipText = path;
item.ImageIndex = 0;
this.listView1.Items.Add(item);
item.EnsureVisible();//滚动条到可见新建的文件夹
item.BeginEdit();//新建完提供即时重命名
}
private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = currentDir.FullName + "\\新建文本文档.txt";
path = filenum(path);
File.CreateText(path);
ListViewItem item = new ListViewItem(path.Substring(stIndexOf('\\') + 1));
item.SubItems.Add(path.Length < 1024 * 1024 ? "" + path.Length / 1024 + "KB" : "" + path.Length / 1024 / 1024 + "MB");
item.SubItems.Add(checkFileType(path));
item.SubItems.Add(File.GetCreationTime(path).ToString("u"));
item.SubItems.Add(File.GetLastWriteTime(path).ToString("u"));
item.SubItems.Add(File.GetLastAccessTime(path).ToString("u"));
item.Tag = "1";
item.ToolTipText = path;
this.listView1.Items.Add(item);
item.EnsureVisible();
item.BeginEdit();
}
private void 重命名MToolStripMenuItem_Click(object sender, EventArgs e) {
listView1.SelectedItems[0].BeginEdit();
}
private void 文件夹FToolStripMenuItem_Click(object sender, EventArgs e) {
文件夹ToolStripMenuItem_Click(sender, e);
}
private void 文本文档ToolStripMenuItem_Click(object sender, EventArgs e) {
文件ToolStripMenuItem_Click(sender,e);
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
if (listView1.SelectedItems.Count == 1)
{
重命名MToolStripMenuItem1.Enabled = true;
删除DToolStripMenuItem.Enabled = true;
剪切CToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem1.Enabled = true;
}
if (listView1.SelectedItems.Count > 1)
{
删除DToolStripMenuItem.Enabled = true;
剪切CToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem1.Enabled = true;
重命名MToolStripMenuItem1.Enabled = false;
}
if (listView1.SelectedItems.Count == 0)
{
剪切CToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem1.Enabled = false;
重命名MToolStripMenuItem1.Enabled = false;
删除DToolStripMenuItem.Enabled = false;
}
}
private void 文件FToolStripMenuItem_Click(object sender, EventArgs e) {
if (listView1.SelectedItems.Count == 1)
{
重命名MToolStripMenuItem.Enabled = true;
删除ToolStripMenuItem.Enabled = true;
}
if (listView1.SelectedItems.Count > 1)
{
删除ToolStripMenuItem.Enabled = true;
重命名MToolStripMenuItem.Enabled = false;
}
if (listView1.SelectedItems.Count == 0)
{
重命名MToolStripMenuItem.Enabled = false;
删除ToolStripMenuItem.Enabled = false;
}
}
private void 重命名MToolStripMenuItem1_Click(object sender, EventArgs e)
{
重命名MToolStripMenuItem_Click(sender, e);
}
private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
文件ToolStripMenuItem_Click(sender, e);
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定要删除吗?", "确认删除", MessageBoxButtons.YesNo, rmation);
if (result == DialogResult.Yes)
{
foreach (ListViewItem temp in listView1.SelectedItems)
{
if (temp.SubItems[1].Text.Equals(""))//判断所要删除的是文件或文件夹
Directory.Delete(currentDir.FullName + "\\" + temp.Text, true);
else
File.Delete(currentDir.FullName + "\\" + temp.Text);
temp.Remove();
}
}
}
private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
{
删除ToolStripMenuItem_Click(sender, e);
}
private void CopyDirectory(string sourceDirName, string destDirName)
{
if (sourceDirName.Substring(sourceDirName.Length - 1) != "\\")
{
sourceDirName = sourceDirName + "\\";//截取出源文件夹目录
}
if (destDirName.Substring(destDirName.Length - 1) != "\\")
{
destDirName = destDirName + "\\";//截取出目标文件夹目录
}
Directory.CreateDirectory(destDirName);//复制文件夹下的子文件夹
foreach (string item in Directory.GetFiles(sourceDirName))//复制文件夹下的文件
{
File.Copy(item, destDirName + Path.GetFileName(item), true);
}
foreach (string item in Directory.GetDirectories(sourceDirName))
{
sourceDirName = item;
destDirName = destDirName.Substring(0, stIndexOf("\\") + 1) + item.Substring(stIndexOf("\\") + 1);
CopyDirectory(sourceDirName, destDirName);//递归完成文件夹下子文件夹和文件复制
}
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
oldpath[0] = "复制";//设置标记复制或剪切
type[0] = "";
int i = 1;
foreach (ListViewItem temp in listView1.SelectedItems)
{
oldpath[i] = currentDir.FullName + "\\" + temp.Text;//存储源路径
if (temp.SubItems[1].Text.Equals(""))//存储源类型以便后续操作
type[i] = "文件夹";
else
type[i] = "文件";
i++;
}
粘贴PToolStripButton.Enabled = true;
粘贴PToolStripMenuItem.Enabled = true;
粘贴PToolStripMenuItem1.Enabled = true;
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
oldpath[0] = "剪切";
type[0] = "";
int i = 1;
foreach (ListViewItem temp in listView1.SelectedItems)
{
oldpath[i] = currentDir.FullName + "\\" + temp.Text;
if (temp.SubItems[1].Text.Equals(""))
type[i] = "文件夹";
else
type[i] = "文件";
i++;
}
粘贴PToolStripButton.Enabled = true;
粘贴PToolStripMenuItem.Enabled = true;
粘贴PToolStripMenuItem1.Enabled = true;
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
if (oldpath[0].Equals("复制"))
{
for (int i = 1; i < oldpath.Length; i++)
{
if (oldpath[i] == null)//路径为空后停止黏贴
break;
newpath[i] = currentDir.FullName + oldpath[i].Substring(oldpath[i].LastIndexOf('\\')+1);//设置新路径名
if (type[i].Equals("文件夹"))
{
if (Directory.Exists(newpath[i]))//判断目标目录下是否有同名文件
newpath[i] = newpath[i] + " - 副本";
newpath[i] = directorynum(newpath[i]);
CopyDirectory(oldpath[i], newpath[i]);
}
else
{
if (File.Exists(newpath[i]))//判断目标目录下是否有同名文件
newpath[i] = newpath[i].Insert(newpath[i].LastIndexOf("."), " - 副本");
newpath[i] = filenum(newpath[i]);
File.Copy(oldpath[i], newpath[i]);
}
}
}
if (oldpath[0].Equals("剪切"))
{
for (int i = 1; i < oldpath.Length; i++)
{
if (oldpath[i] == null)
break;
newpath[i] = currentDir.FullName + oldpath[i].Substring(oldpath[i].LastIndexOf('\\') + 1);
if (type[i].Equals("文件夹"))
{
if (Directory.Exists(newpath[i]))//判断目标目录下是否有同名文件
newpath[i] = newpath[i] + " - 副本";
newpath[i] = directorynum(newpath[i]);
CopyDirectory(oldpath[i], newpath[i]);
Directory.Delete(oldpath[i], true);
}
else
{
if(File.Exists(newpath[i]))//判断目标目录下是否有同名文件
newpath[i] = newpath[i].Insert(newpath[i].Length - 4, " - 副本");
newpath[i] = filenum(newpath[i]);
File.Move(oldpath[i], newpath[i]);
}
}
}
for (int i = 1; i < oldpath.Length; i++)//添加listview的项
{
if (newpath[i] == null)
break;
if (type[i].Equals("文件夹"))
{
ListViewItem item = new ListViewItem(newpath[i].Substring(newpath[i].LastIndexOf('\\') + 1));
item.SubItems.Add("");
item.SubItems.Add("文件夹");
item.SubItems.Add(File.GetCreationTime(newpath[i]).ToString("u"));
item.SubItems.Add(File.GetLastWriteTime(newpath[i]).ToString("u"));
item.SubItems.Add(File.GetLastAccessTime(newpath[i]).ToString("u"));
item.Tag = "0";
item.ToolTipText = newpath[i];
item.ImageIndex = 0;
this.listView1.Items.Add(item);
}
else
{
ListViewItem item = new ListViewItem(newpath[i].Substring(newpath[i].LastIndexOf('\\') + 1));
item.SubItems.Add(newpath[i].Length < 1024 * 1024 ? "" + newpath[i].Length / 1024 + "KB" : "" + newpath[i].Length / 1024 / 1024 + "MB");
item.SubItems.Add(checkFileType(newpath[i]));
item.SubItems.Add(File.GetCreationTime(newpath[i]).ToString("u"));
item.SubItems.Add(File.GetLastWriteTime(newpath[i]).ToString("u"));
item.SubItems.Add(File.GetLastAccessTime(newpath[i]).ToString("u"));
item.Tag = "1";
item.ToolTipText = newpath[i];
this.listView1.Items.Add(item);
}
}
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach(ListViewItem temp in listView1.Items)
temp.Selected = true;
listView1.Focus();
}
private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
{
//判断是否有选定项再启用选项
if (listView1.SelectedItems.Count >= 1)
{
剪切TToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem.Enabled = true;
}
if (listView1.SelectedItems.Count == 0)
{
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
}
}
private void 剪切CToolStripMenuItem_Click(object sender, EventArgs e) {
剪切TToolStripMenuItem_Click(sender, e);
}
private void 复制CToolStripMenuItem1_Click(object sender, EventArgs e) {
复制CToolStripMenuItem_Click(sender,e);
}
private void 粘贴PToolStripMenuItem1_Click(object sender, EventArgs e) {
粘贴PToolStripMenuItem_Click(sender, e);
}
private void 剪切UToolStripButton_Click(object sender, EventArgs e) {
剪切TToolStripMenuItem_Click(sender, e);
}
private void 复制CToolStripButton_Click(object sender, EventArgs e) {
复制CToolStripMenuItem_Click(sender, e);
}
private void 粘贴PToolStripButton_Click(object sender, EventArgs e) {
粘贴PToolStripMenuItem_Click(sender, e);
}
private void viewcheck()//取消所有视图选项
{
详细信息ToolStripMenuItem.Checked = false;
大图标CToolStripMenuItem.Checked = false;
小图标OToolStripMenuItem.Checked = false;
列表ToolStripMenuItem1.Checked = false;
大图标ToolStripMenuItem.Checked = false;
小图标ToolStripMenuItem.Checked = false;
列表ToolStripMenuItem.Checked = false;
详细信息ToolStripMenuItem1.Checked = false;
}
private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
{
viewcheck();
大图标ToolStripMenuItem_Click(sender, e);
大图标CToolStripMenuItem.Checked = true;
大图标ToolStripMenuItem.Checked = true;
}
private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
{
viewcheck();
this.listView1.View = View.SmallIcon;
小图标OToolStripMenuItem.Checked = true;
小图标ToolStripMenuItem.Checked = true;
}
private void 列表ToolStripMenuItem1_Click(object sender, EventArgs e)
{
viewcheck();
this.listView1.View = View.List;
列表ToolStripMenuItem1.Checked = true;
列表ToolStripMenuItem.Checked = true;
}
private void 详细信息ToolStripMenuItem_Click_1(object sender, EventArgs e) {
viewcheck();
this.listView1.View = View.Details;
详细信息ToolStripMenuItem.Checked = true;
详细信息ToolStripMenuItem1.Checked = true;
}
private void addressTB_TextChanged(object sender, EventArgs e)
{
if (ls.Count > 1)//判断是否能后退在启用后退选项
{
toolStripButton2.Enabled = true;
后退ToolStripMenuItem.Enabled = true;
}
if (addressTB.Text.EndsWith("\\"))//判断是否能向上一级在启用向上一级选项{
toolStripButton1.Enabled = false;
向上一级ToolStripMenuItem.Enabled = false;
}
else
{
toolStripButton1.Enabled = true;
向上一级ToolStripMenuItem.Enabled = true;
}
}
private void 后退ToolStripMenuItem_Click(object sender, EventArgs e) {
ind--;
showListView(ls[ind]);
addressTB.Text = ls[ind];
前进ToolStripMenuItem.Enabled = true;
toolStripButton3.Enabled = true;
if (ind == 0)
{
后退ToolStripMenuItem.Enabled = false;
toolStripButton2.Enabled = false;
}
}
private void 前进ToolStripMenuItem_Click(object sender, EventArgs e) {
ind++;
showListView(ls[ind]);
addressTB.Text = ls[ind];
if (ind == ls.Count-1)
{
前进ToolStripMenuItem.Enabled = false;
toolStripButton3.Enabled = false;
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (currentDir.Parent != null)
{
ls.Add(currentDir.Parent.FullName);
ind = ls.Count - 1;
this.addressTB.Text = currentDir.Parent.FullName;
showListView(currentDir.Parent.FullName);
前进ToolStripMenuItem.Enabled = false;
toolStripButton3.Enabled = false;
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
前进ToolStripMenuItem_Click(sender, e);
}
private void 向上一级ToolStripMenuItem_Click(object sender, EventArgs e) {
toolStripButton1_Click(sender, e);
}
private void sortcheck()//取消所有选定项
{
名称ToolStripMenuItem.Checked = false;
大小ToolStripMenuItem.Checked = false;
类型ToolStripMenuItem.Checked = false;
创建时间ToolStripMenuItem.Checked = false;
}
private void 名称ToolStripMenuItem_Click(object sender, EventArgs e) {
sortcheck();
名称ToolStripMenuItem.Checked = true;
sorter.SortColumn = 0;
listView1.Sort();
}
private void 大小ToolStripMenuItem_Click(object sender, EventArgs e) {
sortcheck();
大小ToolStripMenuItem.Checked = true;
sorter.SortColumn = 1;
listView1.Sort();
}
private void 类型ToolStripMenuItem_Click(object sender, EventArgs e) {
sortcheck();
类型ToolStripMenuItem.Checked = true;
sorter.SortColumn = 2;
listView1.Sort();
}
private void 创建时间ToolStripMenuItem_Click(object sender, EventArgs e) {
sortcheck();
创建时间ToolStripMenuItem.Checked = true;
sorter.SortColumn = 4;
listView1.Sort();
}
private void 递增ToolStripMenuItem_Click(object sender, EventArgs e) {
递增ToolStripMenuItem.Checked = true;
递减ToolStripMenuItem.Checked = false;
sorter.SortOrder = SortOrder.Ascending;
listView1.Sort();
}
private void 递减ToolStripMenuItem_Click(object sender, EventArgs e) {
递增ToolStripMenuItem.Checked = false;
递减ToolStripMenuItem.Checked = true;
sorter.SortOrder = SortOrder.Descending;
listView1.Sort();
}
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) {
if (e.Column == this.sorter.SortColumn)
{
if (this.sorter.SortOrder == SortOrder.Ascending)
{
this.sorter.SortOrder = SortOrder.Descending;
递增ToolStripMenuItem.Checked = false;//同步菜单选项
递减ToolStripMenuItem.Checked = true;
}
else
if (this.sorter.SortOrder == SortOrder.Descending)
{
this.sorter.SortOrder = SortOrder.Ascending;
递增ToolStripMenuItem.Checked = true;
递减ToolStripMenuItem.Checked = false;
}
else
return;
}
else
{
this.sorter.SortColumn = e.Column;
}
sortcheck();
if (e.Column == 0)//同步菜单排序选项
名称ToolStripMenuItem.Checked = true;
else if (e.Column == 1)
大小ToolStripMenuItem.Checked = true;
else if (e.Column == 2)
类型ToolStripMenuItem.Checked = true;
else if (e.Column == 4)
创建时间ToolStripMenuItem.Checked = true;
listView1.Sort();
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
toolStripStatusLabel2.Image = null;
toolStripStatusLabel2.Text = "\n" + listView1.Items.Count + "个项目";
}
else if (listView1.SelectedItems.Count == 1)
{
if (listView1.SelectedItems[0].ImageIndex != -1)
toolStripStatusLabel2.Image = imageList2.Images[listView1.SelectedItems[0].ImageIndex];
toolStripStatusLabel2.Text = listView1.SelectedItems[0].Text + " \n修改日期:" + listView1.SelectedItems[0].SubItems[4].Text;
}
else
{
toolStripStatusLabel2.Image = null;
toolStripStatusLabel2.Text = "\n已选择" + listView1.SelectedItems.Count + "项";
}
if (listView1.SelectedIndices.Count !=0)
{
剪切UToolStripButton.Enabled = true;
复制CToolStripButton.Enabled = true;
}
else
{
剪切UToolStripButton.Enabled = false;
复制CToolStripButton.Enabled = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToString();
}
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e) {
if (状态栏ToolStripMenuItem.Checked == true)
{
状态栏ToolStripMenuItem.Checked = false;
statusStrip2.Visible = false;
}
else
{
状态栏ToolStripMenuItem.Checked = true;
statusStrip2.Visible = true;
}
}
private string filenum(string filename)
{
string nums = "";
int num = 1;
while (true)//判断路径下是否已存在文件否则加上序号标签
{
if (File.Exists(filename.Insert(stIndexOf("."), nums + "")))
{
num++;
nums = "(" + num + ")";
}
else
{
filename = filename.Insert(stIndexOf("."), nums + "");
break;
}
}
return filename;
}
private string directorynum(string directoryname)
{
string nums = "";
int num = 1;
while (true)//判断路径下是否已存在该文件夹否则加上序号标签
{
if (Directory.Exists(directoryname + nums))
{
num++;
nums = "(" + num + ")";
}
else
{
directoryname = directoryname + nums;
break;
}
}
return directoryname;
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.Show();
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
Point ptScreen = new Point(e.X, e.Y);
Point pt = listView1.PointToClient(ptScreen);
ListViewItem TargetItem = listView1.GetItemAt(pt.X, pt.Y);//拖动的项将放置于该项之前
if (TargetItem.ToolTipText.Equals(draggedItem.ToolTipText))
return;
if (null != TargetItem && TargetItem.Tag.Equals("0"))
{
if (draggedItem.Tag.Equals("0"))
{
if (!Directory.Exists(TargetItem.ToolTipText + "\\" + draggedItem.Text))
{
CopyDirectory(draggedItem.ToolTipText, TargetItem.ToolTipText + "\\" + draggedItem.Text);
Directory.Delete(draggedItem.ToolTipText, true);
listView1.Items.Remove(draggedItem);
}
else
MessageBox.Show("该目录下已存在同名文件夹", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (!File.Exists(TargetItem.ToolTipText + "\\" + draggedItem.Text))
{
File.Move(draggedItem.ToolTipText, TargetItem.ToolTipText + "\\" + draggedItem.Text);
listView1.Items.Remove(draggedItem);
}
else
MessageBox.Show("该目录下已存在同名文件", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
Point ptScreen = new Point(e.X, e.Y);
Point pt = listView1.PointToClient(ptScreen);
ListViewItem item = listView1.GetItemAt(pt.X, pt.Y);
if (listView1.SelectedItems.Count != 0)
foreach (ListViewItem titem in listView1.SelectedItems)
titem.Selected = false;
if (item != null && item.Tag.Equals("0"))
{
item.Selected = true;。

相关文档
最新文档