using System;
using System.IO;
using System.Threading.Tasks;
namespace SearchObjFiles
{
class Program
{
static async Task Main(string[] args)
{
try
{
// Ask the user to enter the directory to search
Console.WriteLine("请输入要搜索的目录:");
string dir = Console.ReadLine();
// Validate the directory input
if (string.IsNullOrEmpty(dir))
{
throw new ArgumentException("目录不能为空。");
}
if (!Directory.Exists(dir))
{
throw new DirectoryNotFoundException("目录不存在。");
}
// Ask the user to enter the file extension to search for
Console.WriteLine("请输入要搜索的文件扩展名:");
string ext = Console.ReadLine();
// Validate the file extension input
if (string.IsNullOrEmpty(ext))
{
throw new ArgumentException("文件扩展名不能为空。");
}
// Ask the user to enter the output file name
Console.WriteLine("请输入输出文件的名称:");
string output = Console.ReadLine();
// Validate the output file name input
if (string.IsNullOrEmpty(output))
{
throw new ArgumentException("输出文件名称不能为空。");
}
// Ask the user to enter whether to search subdirectories or not
Console.WriteLine("是否要搜索子目录?(y/n)");
string option = Console.ReadLine();
// Validate and parse the option input
SearchOption searchOption;
switch (option.ToLower())
{
case "y":
searchOption = SearchOption.AllDirectories;
break;
case "n":
searchOption = SearchOption.TopDirectoryOnly;
break;
default:
throw new ArgumentException("无效的选项,请输入y或n。");
}
// Get all files with the specified extension in the directory and subdirectories
string[] files = Directory.GetFiles(dir, "*" + ext, searchOption);
// Write the file names and paths to the output file asynchronously
using (StreamWriter sw = new StreamWriter(output))
{
foreach (string file in files)
{
await sw.WriteLineAsync(file);
}
}
// Display a success message
Console.WriteLine("检索完成,共找到{0}个文件,已写入{1}。", files.Length, output);
}
catch (Exception ex)
{
// Display an error message
Console.WriteLine("发生错误:{0}", ex.Message);
}
}
}
}
2023-02-24 ChatGpt 生成检索文件的方法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 首先说说在JAVA中写了一个NATIVE方法,然后生成头文件的方法 javah -classpath java生在...
- 步骤: (1)修改WORKSPACE 配置(sdk & ndk) (2)在tensorflow根目录下 运行 ....
- 背景:在项目中使用了less,用的是vscode中esay less插件,但在每次保存.less文件时,都会在对应...
- 首先将py文件中所有文件路径改为资源访问路径,在python主文件中加入如下函数及类似路径 这样exe文件就无需附...
- 小组学习量化积分约法三章 第一章:平常加分 1.上课积极正确回答问题一人次计1分; 2.每次测验成绩抽阅成绩前六计...