• 文件夹操作


    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 WindowsFormsApplication68
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                listView1.Items.Clear(); //清空ListView控件中的项
                    if(folderBrowserDialog1.ShowDialog()==DialogResult.OK) //判断是否选择了路径
                    {
                        textBox1.Text=folderBrowserDialog1.SelectedPath; //显示选择的路径
                        DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text); //创建DirectoryInfo对象
                        //获取指定目录下的所有子目录及文件类型
                        FileSystemInfo[] fsinfos=dinfo.GetFileSystemInfos();
                        foreach(FileSystemInfo fsinfo in fsinfos)
                        {
                            if(fsinfo is DirectoryInfo) //判断是否是文件夹
                            {
                                //使用获取的文件夹名称创建DirectoryInfo对象
                                DirectoryInfo dirinfo = new DirectoryInfo(fsinfo.FullName);
                                listView1.Items.Add(dirinfo.Name); //为ListView控件添加文件夹信息
                                //显示文件夹名称
                                listView1.Items[listView1.Items.Count - 1].SubItems.Add(dirinfo.FullName);
                                //显示一个空字符串
                                listView1.Items[listView1.Items.Count - 1].SubItems.Add("");
                                //显示文件夹创建时间
                                listView1.Items[listView1.Items.Count - 1].SubItems.Add(dirinfo.CreationTime.ToShortDateString());
                            }
                            else
                            {
                                FileInfo finfo = new FileInfo(fsinfo.FullName); //使用获取的文件名称创建FileInfo对象
                                listView1.Items.Add(finfo.Name); //为ListView控件添加文件信息
                                //显示文件夹名称
                                listView1.Items[listView1.Items.Count - 1].SubItems.Add(finfo.FullName);
                                //显示文件夹大小
                                listView1.Items[listView1.Items.Count - 1].SubItems.Add(finfo.Length.ToString());
                                //显示文件夹创建时间
                                listView1.Items[listView1.Items.Count - 1].SubItems.Add(finfo.CreationTime.ToShortDateString());
                            }
                        }
                  }
            }
        }
    }

  • 相关阅读:
    【JS】修改字体
    【git】.gitignore文件常用设置
    【CSS】让textarea在div里水平垂直都居中的三种办法
    【JS】随着进度条进展,逐个显示li节点
    【MySQL】The server time zone value 'xxxxxx' is unrecognized or represents more than one time zone 解决方案
    浅谈Web前端开发中的Touch事件
    [转]PostgreSQL与MySQL比较
    RDLC:An error occurred during local report processing
    [转]让你的PHP更安全
    [转]Win7或Windows server 2008中IIS7支持ASP+Access解决方法
  • 原文地址:https://www.cnblogs.com/milantgh/p/3723734.html
Copyright © 2020-2023  润新知