文件目录查看工具

C#文件系统操作
本文介绍了一个使用C#实现的简单文件系统浏览器应用,能够显示指定目录下的所有子目录及文件,并提供基本信息如创建时间、大小等。同时支持浏览目录结构、查看文件详情及返回上一级目录等功能。

实现代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp20140821
{
    public partial class FileSystem : Form
    {
        public FileSystem()
        {
            InitializeComponent();
            this.txtDirectoryInfo.Text = "F:\\";
        }

        private void BtnGet_Click(object sender, EventArgs e)
        {
            ClearAll();
            string folderName = this.txtDirectoryInfo.Text;
            this.txtCurrentDirectoryInfo.Text = folderName;
            try
            {
                DirectoryInfo folder = new DirectoryInfo(folderName);
                if(folder.Exists)
                {
                    GetDirectoryInfo(folderName);
                }

                FileInfo fileInfo = new FileInfo(folderName);
                if(fileInfo.Exists)
                {
                    GetFileInfo(folderName);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Message");
            }
        }

        private void GetFileInfo(string fileName)
        {
            FileInfo fileInfo = new FileInfo(fileName);
            if(fileInfo.Exists)
            {
                this.txtCreationTime.Text = fileInfo.CreationTime.ToLongDateString() + " "
                    + fileInfo.CreationTime.ToLongTimeString();
                this.txtFileSize.Text = fileInfo.Length.ToString() + " bytes";
                this.txtLastAccessTime.Text = fileInfo.LastAccessTime.ToLongDateString();
                this.txtLastWriteTime.Text = fileInfo.LastWriteTime.ToLongDateString();
            }
        }

        private void GetDirectoryInfo(string folderName)
        {
            DirectoryInfo folder = new DirectoryInfo(folderName);
            if(folder.Exists)
            {
                this.listBoxDirectoryInfos.Items.Clear();
                foreach (DirectoryInfo info in folder.GetDirectories())
                {
                    this.listBoxDirectoryInfos.Items.Add(info.Name);
                }
                this.listBoxFileInfos.Items.Clear();
                foreach(FileInfo info in folder.GetFiles())
                {
                    this.listBoxFileInfos.Items.Add(info.Name);
                }
            }      
        }
        private void ClearAll()
        {
            this.txtCreationTime.Text = "";
            this.txtFileSize.Text = "";
            this.txtLastAccessTime.Text = "";
            this.txtLastWriteTime.Text = "";
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            ClearAll();
            string currentFolderName = this.txtCurrentDirectoryInfo.Text;
            DirectoryInfo folder = new DirectoryInfo(currentFolderName);
            if (folder.Parent == null)
            {
                return;
            }
            currentFolderName = folder.Parent.FullName;
            this.txtCurrentDirectoryInfo.Text = currentFolderName;
            if (folder.Exists)
            {
                GetDirectoryInfo(currentFolderName);
            }
        }

        private void listBoxDirectoryInfos_MouseClick(object sender, MouseEventArgs e)
        {
            int index = listBoxDirectoryInfos.IndexFromPoint(e.X, e.Y);
            if (index != -1)
            {
                ClearAll();
                string fileName = this.listBoxDirectoryInfos.SelectedItem.ToString();
                string folderName = Path.Combine(this.txtCurrentDirectoryInfo.Text, fileName);
                this.txtCurrentDirectoryInfo.Text = folderName;
                DirectoryInfo info = new DirectoryInfo(folderName);
                if(info.Exists)
                {
                    GetDirectoryInfo(folderName);
                }
                FileInfo fileInfo = new FileInfo(folderName);
                if (fileInfo.Exists)
                {
                    GetFileInfo(folderName);
                }
            }
        }

        private void listBoxFileInfos_MouseClick(object sender, MouseEventArgs e)
        {
            int index = listBoxFileInfos.IndexFromPoint(e.X, e.Y);
            if (index != -1)
            {
                ClearAll();
                string fileName = this.listBoxFileInfos.SelectedItem.ToString();
                string folderName = Path.Combine(this.txtCurrentDirectoryInfo.Text, fileName);
                
                FileInfo fileInfo = new FileInfo(folderName);
                if (fileInfo.Exists)
                {
                    GetFileInfo(folderName);
                }
            }
        }

        private void txtDirectoryInfo_MouseClick(object sender, MouseEventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowDialog();
            this.txtDirectoryInfo.Text = dialog.SelectedPath;
            BtnGet_Click(this, new EventArgs());
        }

        private void txtDirectoryInfo_TextChanged(object sender, EventArgs e)
        {
            this.txtCurrentDirectoryInfo.Text = this.txtDirectoryInfo.Text;
        }
    }
}

实现效果:

转载:http://blog.youkuaiyun.com/foreverling/article/details/38823687

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值