文件目录查看工具

简介: 实现代码: 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 Windo

实现代码:

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;
        }
    }
}

实现效果:


目录
相关文章
|
7月前
|
Linux 编译器
目录文件篇
目录文件篇
|
6月前
|
监控 Windows
文件目录管理
文件目录管理
|
7月前
|
弹性计算 运维 Shell
45 # 实现文件拷贝功能
45 # 实现文件拷贝功能
53 0
|
Linux Shell
6.2 文件与目录管理
6.2 文件与目录管理
67 0
|
JavaScript Windows
XRename(文件文件夹超级重命名工具)简介
XRename(文件文件夹超级重命名工具)简介
185 0
XRename(文件文件夹超级重命名工具)简介
|
开发者 Python
文件拷贝功能 | 学习笔记
快速学习 文件拷贝功能
114 0
文件拷贝功能 | 学习笔记
|
监控 API
C#-FileSystemWatcher文件和文件夹监控
FileSystemWatcher这个类用于当目录或目录中的文件发生更改时,侦听文件系统更改通知并引发事件
141 0
|
Linux Shell 开发者
文件目录命令 | 学习笔记
快速学习文件目录命令。
105 0
压缩软件能否加上忽略某些目录的功能
压缩软件能否加上忽略某些目录的功能
138 0