C#简单文件管理器的实现
来源:互联网 发布:安居客盗图软件 编辑:程序博客网 时间:2024/05/29 11:07
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Text;using System.Windows.Forms;namespace SimpleFileManager{ public partial class Form1 : Form { public Form1() { InitializeComponent(); Initialize(); } public bool mInitialized = false;//定义一bool变量看窗口是否已初始化 private void Initialize() { try { if (mInitialized)//查看窗口是否已初始化 return; this.tb_CurPath.Text = Application.StartupPath; lst_Files.View = View.Details; //添加列 lst_Files.Columns.Add("文件名称", lst_Files.Width / 3, HorizontalAlignment.Left); lst_Files.Columns.Add("目录/文件", lst_Files.Width / 6, HorizontalAlignment.Center); lst_Files.Columns.Add("文件大小", lst_Files.Width / 6, HorizontalAlignment.Right); lst_Files.Columns.Add("最后存取时间", lst_Files.Width / 3, HorizontalAlignment.Left); FillFiles(); mInitialized = true; } catch(System.Exception E) { ErrorHandler(E.ToString()); } } private string GetInitDir() { return Application.StartupPath; } private void ErrorHandler(string ErrorDescription) { MessageBox.Show(ErrorDescription); } private void FillFiles() { //将文件列表写入Listview lst_Files.Items.Clear(); DirectoryInfo cd = new DirectoryInfo(this.tb_CurPath.Text + "\\"); foreach (DirectoryInfo d in cd.GetDirectories()) { lst_Files.Items.Add(new ListViewItem(new string[]{d.Name,"目录","",d.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss",null)})); } foreach(FileInfo f in cd.GetFiles()) { lst_Files.Items.Add(new ListViewItem(new string[] { f.Name, "档案", f.Length.ToString(), f.LastWriteTime.ToString("yyyy-MM-dd hh:mm:ss", null) })); } } private void btn_Up_Click(object sender, EventArgs e) { if (this.tb_CurPath.Text.LastIndexOf("\\") != -1) { this.tb_CurPath.Text = this.tb_CurPath.Text.Substring(0, this.tb_CurPath.Text.LastIndexOf("\\")); FillFiles(); } } private void btn_CreateFolder_Click(object sender, EventArgs e) { if(tb_Name.Text=="") { MessageBox.Show("目录名称不可空白"); } if(Directory.Exists(tb_CurPath.Text+"\\"+tb_Name.Text)) { MessageBox.Show("目录["+tb_CurPath.Text+"\\"+tb_Name.Text+"]已存在"); return; } Directory.CreateDirectory(tb_CurPath.Text+"\\"+tb_Name.Text); FillFiles(); } private void btn_CreateFile_Click(object sender, EventArgs e) { if(tb_Name.Text=="") { MessageBox.Show("文件名不能为空"); } if(File.Exists(tb_CurPath.Text+"//"+tb_Name.Text)) { MessageBox.Show("文件["+tb_CurPath.Text+"\\"+tb_Name.Text+"[已存在"); } FileStream fs =File.Create(tb_CurPath.Text + "\\" + tb_Name.Text); fs.Close(); FillFiles(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { try { if (lst_Files.SelectedItems.Count == 0) return; if(lst_Files.SelectedItems[0].SubItems[1].Text=="目录") { string strDir = tb_CurPath.Text + "\\" + lst_Files.SelectedItems[0].Text; DialogResult dr = MessageBox.Show("确定删除目录[" + strDir + "]?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question); if(dr==DialogResult.OK) { Directory.Delete(strDir, false); FillFiles(); MessageBox.Show("目录[" + strDir + "]已删除!"); } } else { string strFile = tb_CurPath.Text + "//" + lst_Files.SelectedItems[0].Text; DialogResult dr = MessageBox.Show("确定删除档案[" + strFile + "]吗?"); if(dr==DialogResult.OK) { File.Delete(strFile); FillFiles(); MessageBox.Show("文档[" + strFile + "已删除!"); } } } catch(Exception E) { ErrorHandler(E.ToString()); } } }}
右键菜单时得注意 Form窗口的 ContextMenuStrip属性必须填入 新建的ContextMenuStrip名,要不然不出现右键菜单。。。
我太菜! !! !!!
0 0
- C#简单文件管理器的实现
- C#实现文件管理器
- C#实现文件管理器
- android 简单文件管理器的实现
- 文件管理器的简单实现原理
- 简单的文件管理器
- 【desalbres】使用MVC3和jsTree实现的简单文件管理器
- 一个简单的文件管理器
- Android自定义适配器---实现简单文件管理器
- 利用C#制作简单的成绩管理器
- 简单的Web形式文件管理器
- C#实现的一个任务管理器
- 读取linux /proc/文件信息,用GTK编程实现简单的系统管理器
- 任务管理器简单实现
- android文件管理器的源码实现
- android 文件管理器的源码实现
- 异步任务的实现之文件管理器
- 简易文件管理器实现
- JVM调优总结(七)-典型配置举例1
- struts2自定义类型转换器:DefaultTypeConverter
- setsockopt设置socket状态
- 杭电OJ:1010 Tempter of the Bone
- UML之构件图
- C#简单文件管理器的实现
- java将文件打包成ZIP压缩文件的工具类实例
- 如何学好C++语言/C语言/
- 页面果滚动效果
- android国际化
- 线程间同步之 Mutex(mutual exclusion 互斥量)
- TDD: 解除依赖
- 为什么赋值运算符要防止自身赋值
- 电脑故障导致数据丢失如何恢复?