文件目录查看工具

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

实现效果:


目录
相关文章
|
15天前
|
Linux 编译器
目录文件篇
目录文件篇
|
6月前
45 # 实现文件拷贝功能
45 # 实现文件拷贝功能
28 0
|
8月前
|
Linux Shell
6.2 文件与目录管理
6.2 文件与目录管理
42 0
|
8月前
|
程序员 C++
C++程序文件的目录结构
C++程序文件的目录结构
149 0
|
监控 API
C#-FileSystemWatcher文件和文件夹监控
FileSystemWatcher这个类用于当目录或目录中的文件发生更改时,侦听文件系统更改通知并引发事件
109 0
|
JavaScript Windows
XRename(文件文件夹超级重命名工具)简介
XRename(文件文件夹超级重命名工具)简介
148 0
XRename(文件文件夹超级重命名工具)简介
|
开发者 Python
文件拷贝功能 | 学习笔记
快速学习 文件拷贝功能
81 0
文件拷贝功能 | 学习笔记
|
Linux Shell 开发者
文件目录命令 | 学习笔记
快速学习文件目录命令。
压缩软件能否加上忽略某些目录的功能
压缩软件能否加上忽略某些目录的功能
113 0
|
开发工具 数据库 Perl
文件目录管理
一、目录结构 /boot:系统启动相关的文件,如内核、initrd,以及grub(bootloader) /dev:设备文件 块设备:随机访问,数据块 字符设备:线性访问,按字符为单位 设备号:主设备号(major)和次设备号(minor) /dev/null:软件设备,bit ...
1077 0

热门文章

最新文章