ModelingDebug/SearchFile.cs

169 lines
4.6 KiB
C#

using Debug;
/// <summary>
/// 文件夹文件搜索引擎操作
/// </summary>
namespace Debug;
public class Mod
{
private static DateTime time;
/// <summary>
/// 结束时间
/// </summary>
private static void EndTime()
{
Console.WriteLine("结束" + (DateTime.Now - time).TotalMilliseconds + "ms");
}
/// <summary>
/// 开始时间
/// </summary>
private static void StartTime()
{
time = DateTime.Now;
}
// 是否过滤隐藏文件夹
private readonly bool Isdot = true;
//过滤系统
private readonly List<string> ExcludeFile = [
"Android", "androidword", "Java",
"Linux", "linuxword", "Mac",
"Windows", "Windows 10", "Steam",
"Genshin Impact Game", "Godot", "Gradle",
"WeGame", "Mental Omega", "WeGameApps",
"ramboplayGame", "QQ", "QQWORD","Microsoft SDKs","Creator","editors","AndroidSDK","SDK","Pr","PR","PS","Ps","WE",
"androidstudioword","xshell7"
];
private readonly bool isExcludeFile = true;
public string FileDir = string.Empty;
public Mod()
{
Console.Clear();
ModeSwitch.New("搜索模式", ["返回主页", "文件", "文件夹"], MODE);
}
public void MODE(int a)
{
Console.Clear();
if (a == 0)
{
_ = new Program();
return;
}
if (a == 1)
{
isFile = true;
}
else if (a == 2)
{
isFile = false;
}
Console.Write($"请输入{(isFile ? "" : "")}名称:");
var v = Console.ReadLine();
if (v == null || string.IsNullOrEmpty(v))
{
Console.Write("输入错误");
_ = new Mod();
return;
}
fileName = v;
Console.Clear();
Init();
}
private bool isFile = true;//是否搜索的是文件
private string fileName = string.Empty;
private void Init()
{
//开始计时
StartTime();
//获取所有驱动器
var allDrives = DriveInfo.GetDrives();
foreach (var d in allDrives)
{
Console.Clear();
//判断是否可读
if (d.IsReady == true)
{
var name = d.Name;
if (name.Contains("C:\\"))//排除系统盘
{
continue;
}
FileSearch(name);
}
//结束计时
EndTime();
Yes();
}
}
private void FileSearch(string path)
{
try
{
//获取文件夹
DirectoryInfo dirInfo = new(path);
foreach (var directory in dirInfo.EnumerateDirectories())
{
Console.Clear();
Console.WriteLine("正在搜索:" + directory);
//判断是否有访问权限
if (!directory.Exists)
{
Console.WriteLine("没有访问的权限 " + directory);
continue;
}
//获取名称
var name = directory.Name;
// 是否过滤掉隐藏文件夹
if (Isdot && name.StartsWith('.'))
{
continue;
}
if (isExcludeFile && ExcludeFile.Contains(name))
{
continue;
}
if (name.Equals(fileName) && directory.Exists)
{
FileDir = directory.FullName;
return;
}
FileSearch(directory.FullName);
}
//处理文件
if (isFile)
{
foreach (var file in dirInfo.EnumerateFiles())
{
Console.Clear();
Console.WriteLine("正在搜索:" + file.FullName);
// 处理文件
if (file.Name.Equals(fileName) && file.Exists)
{
FileDir = file.FullName;
return;
}
}
}
return;
}
catch (Exception ex)
{
//处理异常
Console.WriteLine($"Error accessing directory '{path}': {ex.Message}");
}
}
private void Yes()
{
if (!string.IsNullOrEmpty(FileDir))
{
Console.Write($"已经找到{(isFile ? "" : "")} {FileDir}");
}
else
{
Console.Write($"未找到 {fileName}:{(isFile ? "" : "")}");
}
}
}