如何查找特定目录下的特定文件
本文关键字:文件 何查找 查找 | 更新日期: 2023-09-27 18:03:57
我一直在讨论如何验证目录是否存在以及如果存在则搜索特定文件。我已经做了各种尝试,但到目前为止,我最大的障碍是通过访问修饰符来维护安全性,同时保留我试图完成的模块化特性。下面代码的目标:
- 获取用户的驱动器以搜索特定目录
- 搜索驱动器查找用户的steam目录。
- 查找各种可执行文件;在这种情况下,三款最新的《上古卷轴》游戏。
- 一旦这些已被检测和验证,执行各种操作(发射,检查问题等)
是的,我知道这些东西已经存在了,但我只是想获得一些关于如何执行这种性质的任务的观点。请随意提出建设性的建议和意见,其他的都将被忽略。
private void button1_Click(object sender, EventArgs e)
{
string[] drives = Directory.GetLogicalDrives();
foreach (string d in drives)
{
TW_Detection.IDetection.arLoop((File.Exists(d + sd + g)) ? MessageBox.Show(gn + " has been detected in " + d + sd + g).ToString() : MessageBox.Show("The game was not detected!").ToString());
}
}
}
}
下一段代码是我尝试用各种方法创建最短可能的方法来执行一系列自动目录搜索以找到可执行文件。
#region initialDetection
public class initialDetection
{
private string[] games = { "morrowind''Morrowind.exe", "oblivion''Oblivion.exe", "skyrim''TESV.exe" };
private string[] games_names = { "The Elder Scrolls III: Morrowind", "The Elder Scrolls IV: Oblivion", "The Elder Scrolls V: Skyrim" };
private string[] steamDirectories = { "Program Files''steam''steamapps''common", "Program Files (x86)''steam''steamapps''common", "steam''steamapps''common" };
private void arLoop()
{
for (int i = 0; i < 2; i++)
{
string g = games[i];
string gn = games[i];
}
for (int i = 0; i < 2; i++)
{
string sd = steamDirectories[i];
}
}
}
#endregion
#region initialDetection
public class gameDetection : steamDetection
{
private string[] games = { "morrowind''Morrowind.exe", "oblivion''Oblivion.exe", "skyrim''TESV.exe" };
private string[] games_names = { "The Elder Scrolls III: Morrowind", "The Elder Scrolls IV: Oblivion", "The Elder Scrolls V: Skyrim" };
public partial void arLoop()
{
for (int i = 0; i < 2; i++)
{
string g = games[i];
string gn = games_names[i];
}
}
}
#endregion
#region precompileDetection
public class precompileDetection : gameDetection
{
static string completeDirectory()
{
arLoop();
}
}
#endregion
#region steamDetection
public class steamDetection
{
private string[] steamDirectories = { "Program Files''steam''steamapps''common''", "Program Files (x86)''steam''steamapps''common''", "steam''steamapps''common''" };
public partial void arLoop()
{
for (int i = 0; i < 2; i++)
{
string sd = steamDirectories[i];
}
}
}
#endregion
#region Detection Interface
public interface IDetection
{
void arLoop();
}
#endregion
}
是的,我意识到这段代码非常混乱,并且包含了许多不必要的和破碎的解决方案,这是我的实验的结果。问题在于能够使用检测结果在消息框中显示,告诉用户游戏未被检测到或已经检测到,然后显示已检测到的游戏名称。
private string[] games = { "Morrowind.exe", "Oblivion.exe", "TESV.exe" };
public void findGameDirectories(String startDir)
{
var games = from filePath in Directory.EnumerateFiles(startDir, "*.exe", SearchOption.AllDirectory)
let fileName = Path.FileName(filePath)
where games.Contains(fileName, StringComparison.InvariantCultureIgnoreCase)
select filePath;
}
您可以使用递归解决方案,如下所示。
private string[] games = { "Morrowind.exe", "Oblivion.exe", "TESV.exe" };
public void findGameDirectories(String startDir)
{
foreach (var file in Directory.GetFiles(startDir))
{
foreach (var gameFileName in games)
{
if (file.EndsWith(gameFileName))
{
// You found the game
}
}
}
foreach (var dir in Directory.GetDirectories(startDir))
{
// Search the subdirectories
findGameDirectories(dir);
}
}
这将在您选择的文件夹中启动,并检查该文件夹中的所有文件。然后,它将遍历每个子目录,并检查子目录中的每个文件,以此类推,直到子目录用完为止。
你可能想告诉循环停止一旦它找到所有的游戏,但我将留给你:)
编辑:我刚刚发现c#有一个非常简洁的方法来列出所有文件,包括子目录
public void findGameDirectories(String startDir)
{
DirectoryInfo directory = new DirectoryInfo(startDir);
foreach (var file in directory.GetFiles("*.exe", SearchOption.AllDirectories))
{
foreach (var gameFileName in games)
{
if (file.Name == (gameFileName))
{
Console.WriteLine("You found " + game.DisplayName);
}
}
}
}