[英]How to find specific files in certain directories?
我一直被困在如何验证目录是否存在以及如何搜索特定文件的问题上。 我进行了各种尝试,但是到目前为止,我最大的障碍是通过访问修饰符维护安全性,同时保留我要完成的模块性。 以下代码的目标:
是的,我知道这些事情已经存在,但是我只是想对如何执行这种性质的任务有一些看法。 可以就需要更改的内容提出建设性的建议和评论,所有其他内容都将被忽略。
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.