C#文件操作,父目录,子目录,所有文件、文件夹,属性信息

 

 

下面代码主要参考了C#高级编程第六版的源码部分改写增加了中文注释,

using System;
using System.IO;
using System.Windows.Forms;

namespace FileProperties
{
    public partial class Form1 : Form
    {
        private string currentFolderPath;

        public Form1()
        {
            InitializeComponent();
        }

        protected void ClearAllFields()
        {
            listBoxFolders.Items.Clear();
            listBoxFiles.Items.Clear();
            txtBoxFolder.Text = "";
            txtBoxFileName.Text = "";
            txtBoxCreationTime.Text = "";
            txtBoxLastAccessTime.Text = "";
            txtBoxLastWriteTime.Text = "";
            txtBoxFileSize.Text = "";
        }
        /// <summary>
        /// 显示文件信息
        /// </summary>
        /// <param name="fileFullName">文件完整路径+文件名</param>
        protected void DisplayFileInfo(string fileFullName)
        {
            FileInfo theFile = new FileInfo(fileFullName);
            if (!theFile.Exists)
                throw new FileNotFoundException("文件: " + fileFullName + "没有找到!");
            txtBoxFileName.Text = theFile.Name;
            txtBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString();
            txtBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString();
            txtBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString();
            txtBoxFileSize.Text = theFile.Length + " bytes";
        }
        /// <summary>
        /// 根据目录名显示其下所有文件、文件夹
        /// </summary>
        /// <param name="folderFullName"></param>
        protected void DisplayFolderList(string folderFullName)
        {
            //先判断这个目录存在不
            DirectoryInfo theFolder = new DirectoryInfo(folderFullName);
            if (!theFolder.Exists)
                throw new DirectoryNotFoundException("Folder not found: "
                                                     + folderFullName);
            ClearAllFields();
            txtBoxFolder.Text = theFolder.FullName;
            currentFolderPath = theFolder.FullName;//当前路径设为全局,便于后面操作

            //获取当前目录下的文件夹
            foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())
                listBoxFolders.Items.Add(nextFolder.Name);

            //获取当前目录下的文件
            foreach (FileInfo nextFile in theFolder.GetFiles())
                listBoxFiles.Items.Add(nextFile.Name);
        }

        protected void OnDisplayButtonClick(object sender , EventArgs e)
        {
            try
            {
                ///判断是否是文件夹,是 直接返回。否则 继续
                string folderPath = txtBoxInput.Text;
                DirectoryInfo theFolder = new DirectoryInfo(folderPath);
                if (theFolder.Exists)
                {
                    DisplayFolderList(theFolder.FullName);
                    return;
                }
                //判断是否文件
                FileInfo theFile = new FileInfo(folderPath);
                if (theFile.Exists)
                {
                    //FileInfo.Directory 获取当前文件所在的目录
                    DisplayFolderList(theFile.Directory.FullName);
                    //获取当前文件在文件系统中的索引
                    int index = listBoxFiles.Items.IndexOf(theFile.Name);
                    //设置其选择触发选择事件
                    listBoxFiles.SetSelected(index , true);
                    return;
                }
                throw new FileNotFoundException("没有文件或文件夹的名字是 "
                                                + ": " + txtBoxInput.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 文件被选择时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnListBoxFilesSelected(object sender , EventArgs e)
        {
            try
            {
                string selectedString = listBoxFiles.SelectedItem.ToString();
                string fullFileName = Path.Combine(currentFolderPath , selectedString);
                DisplayFileInfo(fullFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //目录被选择时触发
        protected void OnListBoxFoldersSelected(object sender , EventArgs e)
        {
            try
            {
                //获取选择的目录名
                string selectedString = listBoxFolders.SelectedItem.ToString();
                //
                string fullPathName = Path.Combine(currentFolderPath , selectedString);
                DisplayFolderList(fullPathName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        protected void OnUpButtonClick(object sender , EventArgs e)
        {
            try
            {
                //获取当前目录的父目录
                string folderPath = new FileInfo(currentFolderPath).DirectoryName;
                DisplayFolderList(folderPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

http://hi.baidu.com/jiang_yy_jiang/blog/item/f14c545137646b3143a75bd6.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值