




已阅读5页,还剩35页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验5 文件管理器【实验目的】n 进一步实践windows窗口程序开发的流程;n 掌握并熟练使用后台代码添加控件并定义布局和格式以及交互的技能。【实验环境】 Visual Studio 2005(或更高版本)【实验内容】设计一个基于后台代码添加控件的方式来实现文件管理器的显示和交互功能的Winform程序,鼓励扩展其他功能。基本功能如下图:【实验记录】文件菜单:新建文件夹:提供即时重命名新建文件夹遇到相同明添加序号新建文本文档新建文本文档遇到相同明添加序号选中单个项目激活重命名和删除选中多个项目禁用重命名编辑菜单:(撤销功能未完成)选择项目激活剪切和复制 状态栏显示选择项信息点击复制或剪切激活 复制在同目录下黏贴 同名添加“ - 副本”点击全选查看菜单:点击状态栏 默认打开点击后关闭点击大图标:点击小图标点击详细信息排序方式 类型 点击列头同样实现 排序方式 递减转至:前进禁用点击后退 前进启用点击前进点击向上一级:向上到磁盘跟目录 禁用向上一级帮助关于:工具栏与菜单栏选定状态同步 功能相同右键菜单:启用状态与菜单栏工具栏同步 功能相同本软件提供单项(多项未完成 请勿测试)拖拽移动项目 无法截图展示请自行测试【核心代码】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.Security.AccessControl;using Microsoft.Win32;using System.IO;using System.Collections;namespace FileExplorer public partial class Form1 : Form static DirectoryInfo currentDir; string newpath = new string1024; string oldpath = new string1024; string type = new string1024; List ls = new List(); 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(d.Name); root.Tag = d.Name; 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(childDir.Name); 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(dir.Name); / 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(dir.Name); / 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(dir.Name); / 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(file.Name); item.SubItems.Add(file.Length1024*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(file.Name) / / this.imageList1.Images.Add(Path.GetExtension(file.Name), 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=View.LargeIcon) item.ImageIndex = this.imageList2.Images.Count - 1; if (this.listView1.View = View.LargeIcon) 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(file.Name); 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 = View.LargeIcon; 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.SelectedItems0.Text; e.CancelEdit = false; if (e.Label = null)/取消重命名则退出编辑 e.CancelEdit = true; else if (Directory.Exists(currentDir.FullName + + e.Label) MessageBox.Show(此目标已包含名为 + e.Label + , 重命名失败, MessageBoxButtons.OK, MessageBoxIcon.Warning); e.CancelEdit = true;/同名则自动取消重命名恢复原有命名 else Directory.Move(currentDir.FullName + + temp, currentDir.FullName + + e.Label); private void 文件夹ToolStripMenuItem_Click(object sender, EventArgs e) string path = currentDir.FullName + 新建文件夹; path = directorynum(path); Directory.CreateDirectory(path); ListViewItem item = new ListViewItem(path.Substring(path.LastIndexOf() + 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(path.LastIndexOf() + 1); item.SubItems.Add(path.Length 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, MessageBoxIcon.Information); if (result = DialogResult.Yes) foreach (ListViewItem temp in listView1.SelectedItems) if (temp.SubItems1.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.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年人力资源专员招聘面试模拟题及答案详解
- 第13课 清朝前中期的鼎盛与危机 课件 统编版必修中外历史纲要上
- 鄂尔多斯辅警考试题型及答案
- 人力资源招聘与培训工具包
- 大学毕业论文(设计)致谢9篇
- 2025年企业管理资料金融企业劳动合同示范文文档范本
- 毕业论文(设计)致谢9篇
- 2025年金融机构外汇流动资金借款合同范本
- 《2025全面的新员工个人劳动合同模板集》
- 一、启动Excel建立新工作簿教学设计-2025-2026学年初中信息技术沪科版八年级上册-沪科版
- 铝单板幕墙专项方案
- GB/T 42062-2022医疗器械风险管理对医疗器械的应用
- WB/T 1066-2017货架安装及验收技术条件
- 第六节-食品新资源的开发-课件
- 复变函数-西安交大版课件
- 南方医大护理学导论教案
- 隧道断面施工放样测量记录表
- 小说写作的基础知识短篇小说的写作
- (中职中专)《电控发动机维修》全套教学设计全书电子教案整本书教案合集1-22章全
- 部编版六年级上册道德与法治全册教学课件ppt
- 动物繁殖学复习习题
评论
0/150
提交评论