我有以下代码,应该在应用程序的主目录中递归查找我的文件,包括子文件夹:
public static List<string> FindFileInDirectory(string filename)
{
List<string> output = new List<string>();
try
{
foreach(string d in Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory))
{
foreach (string f in Directory.GetFiles(d, filename))
{
output.Add(f);
}
FindFileInDirectory(d);
}
}catch(System.Exception anyEx)
{
Debug.Print(anyEx.Message);
}
return output;
}
有技术人员可以指导我解决这个问题吗?
错误:
mscorlib.dll中出现未处理的“System.ArgumentOutOfRangeException”类型异常
附加信息:索引指向允许范围之外。索引不能为负数,必须小于集合的大小。
发生的地方:
我有一个Mongoose
http服务器,它只是一个exe,我希望在我的目录中找到它来启动它(如果还没有运行)。我包含了我班级的constructor
,其中只有一些字段易于使用。问题在于找到here的搜索。
代码是:
public MongooseHttpServer()
{
process = Process.GetProcessesByName("mongoose-free-6.5.exe");
if (process.Length == 0)
{
//http server not running, have to start
Debug.Print("not running, but starting now...");
process = new Process[1];
//in this case the first is enough.
mongooseLocation = DirectorySearcher.FindFileInDirectory("mongoose-free-6.5.exe")[0]; //error here.
process[0] = Process.Start(mongooseLocation ?? Directory.GetCurrentDirectory());
}
else
{
Debug.Print("already running");
}
}