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