基本文件的I/O --创建目录列表

来源:互联网 发布:汉化新世纪-软件 编辑:程序博客网 时间:2024/05/17 22:41

在本示例中,DirectoryInfo 是当前目录,用 ("D:\\") 表示,代码列出了当前目录中具有 .txt 扩展名的所有文件,同时还列出了这些文件的大小、创建时间和名称。

using System;
using System.IO;

namespace 创建目录列表
{
    class Program
    {
        public static void Main(string[] args)
        {
            string path = "D:\\";
            //if (args.Length > 0)
            //{
            //    if (File.Exists(args[0]))//确定文件手否存在
            //    {
            //        path = args[0];
            //    }
            //    else
            //    {
            //        Console.WriteLine("{0} not found; using current directory:", args[0]);
            //    }
            //}
            DirectoryInfo dir = new DirectoryInfo(path);
            foreach (FileInfo f in dir.GetFiles("*.txt"))//FileInfo 类用于,复制、移动、重命名、创建、打开、删除和追加到文件,获取文件大小、属性等
            {
                String name = f.Name;
                long size = f.Length;
                DateTime creationTime = f.CreationTime;
                Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size, creationTime, name);
            }
            Console.ReadKey();
        }
    }
}



MSDN文档:FileInfo 类


0 0
原创粉丝点击