C#遍历一个文件夹下的所有可执行文件

时间:2022-12-29 23:22:12
using System;
using System.IO;

namespace IO操作
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(File.Exists(@"C:\IO.txt"));
Console.WriteLine(Directory.Exists(@"C:\"));
Console.WriteLine();

//获取一个路径下所有可执行文件(.exe)
string path = ".";//获取当前路径
if (args.Length > 0)
{
if(Directory.Exists(args[0]))
path = args[0];//从外部获取的参数
else
Console.WriteLine("{0} is not found", args[0]);
}
DirectoryInfo dir=new DirectoryInfo(path);
foreach(FileInfo f in dir.GetFiles("*.exe"))
{
string name=f.Name;
long size=f.Length;
DateTime time=f.CreationTime;
Console.WriteLine("{0,-12:N0}{1,-20:g}{2}",size,time,name);
}

}
}
}