飞道的博客

【C#】获取文件及文件夹信息

302人阅读  评论(0)

【C#】获取文件及文件夹信息

1 题目描述

(1)Form1窗体设计界面如下:

(2)程序运行时,窗体中仅“选择文件”按钮可用,用户单击该按钮,弹出“打开文件”对话框,可选择任一文件,则显示该文件的完整路径;同时“获取文件信息”按钮可用;
(3)单击“获取文件信息”按钮后,显示该文件的创建时间、父目录、根目录、扩展名等信息;

2 源码详解

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

namespace Csharp10_1
{
   
    public partial class Form1 : Form
    {
   
        public Form1()
        {
   
            InitializeComponent();
        }

        private string fileName = "";

        private void SelectFile_Click(object sender, EventArgs e)
        {
   
            // 开启Dialog框
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = ("请选择文件");
            fileDialog.Multiselect = false;
            fileDialog.Filter = "所有文件 (*.*)|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
   
                fileName = fileDialog.FileName;
                this.FileNameText.Text = fileName;
            }
            CreationTimeText.Text = "请单击获取文件信息";
            FolderText.Text = "请单击获取文件信息";
            FolderNameText.Text = "请单击获取文件信息";
            ExtensionText.Text = "请单击获取文件信息";
            GetFileInformation.Enabled = true;
        }

        private void GetFileInformation_Click(object sender, EventArgs e)
        {
   
            System.IO.DirectoryInfo fold = new System.IO.DirectoryInfo(fileName);
            DateTime t = fold.CreationTime;
            this.CreationTimeText.Text = t.ToString();
            this.FolderText.Text = Path.GetPathRoot(fileName);
            this.FolderNameText.Text = Path.GetFileNameWithoutExtension(fileName);
            this.ExtensionText.Text = Path.GetExtension(fileName);
        }
    }
}

3 实现效果




转载:https://blog.csdn.net/Gyangxixi/article/details/117019800
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场