我一直被困在如何验证目录是否存在以及搜索特定文件(如果存在)。我做了各种尝试,但到目前为止,我最大的障碍是通过访问修饰符保持安全性,同时保留了我正在努力实现的模块化特性。 以下代码的目标:
是的,我知道这些事情已经存在,但我只是想了解一下如何执行这种性质的任务。随意提供关于应该改变什么的建设性建议和意见,所有其他人都将被忽略。
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
}
是的,我意识到这段代码非常混乱,并且包含许多不必要的和破碎的解决方案,这些都是我的实验结果。问题在于能够使用来自检测的结果显示在消息框中,该消息框告诉用户游戏未被检测到或者已经检测到并且然后显示游戏的名称。
答案 0 :(得分:1)
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;
}
答案 1 :(得分:0)
您可以使用递归解决方案,如下所示。
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);
}
}
}
}