C# 如何扫描多个驱动器以查找特定目录?

C# 如何扫描多个驱动器以查找特定目录?,c#,file-io,C#,File Io,我不是寻找特定的文件,而是寻找特定的目录来索引这些目录中的所有文件。我知道如何搜索文件类型和名称,但不知道如何使用目录进行索引 试试这样的方法:(摘自MS代码示例) 公共类StackBasedItemation { 静态void Main(字符串[]参数) { //在命令行或中指定起始文件夹 //项目>属性>调试窗格中的Visual Studio。 TraverseTree(args[0]); Console.WriteLine(“按任意键”); Console.ReadKey(); } 公共静

我不是寻找特定的文件,而是寻找特定的目录来索引这些目录中的所有文件。我知道如何搜索文件类型和名称,但不知道如何使用目录进行索引

试试这样的方法:(摘自MS代码示例)

公共类StackBasedItemation
{
静态void Main(字符串[]参数)
{
//在命令行或中指定起始文件夹
//项目>属性>调试窗格中的Visual Studio。
TraverseTree(args[0]);
Console.WriteLine(“按任意键”);
Console.ReadKey();
}
公共静态void TraverseTree(字符串根)
{
//用于保存要删除的子文件夹名称的数据结构
//检查文件。
堆栈dirs=新堆栈(20);
如果(!System.IO.Directory.Exists(root))
{
抛出新ArgumentException();
}
直接推力(根);
而(dirs.Count>0)
{
字符串currentDir=dirs.Pop();
字符串[]子字段;
尝试
{
subDirs=System.IO.Directory.GetDirectories(currentDir);
}
//如果没有,将引发UnauthorizedAccessException异常
//文件夹或文件的发现权限。该权限可能不可接受,也可能不可接受
//忽略异常并继续枚举其余文件和
//DirectoryNotFound异常也是可能的(但不太可能)
//将引发。如果currentDir已被删除,则会发生这种情况
//在我们调用Directory.Exists之后,存在另一个应用程序或线程
//捕获哪些异常的选择完全取决于特定任务
//你打算表演,也取决于你确定知道多少
//关于将在其上运行此代码的系统。
捕获(未经授权的访问例外)
{                    
控制台写入线(e.Message);
继续;
}
捕获(System.IO.DirectoryNotFounde异常)
{
控制台写入线(e.Message);
继续;
}
string[]files=null;
尝试
{
files=System.IO.Directory.GetFiles(currentDir);
}
捕获(未经授权的访问例外)
{
控制台写入线(e.Message);
继续;
}
捕获(System.IO.DirectoryNotFounde异常)
{
控制台写入线(e.Message);
继续;
}
//在此处对每个文件执行所需操作。
//修改此块以执行所需的任务。
foreach(文件中的字符串文件)
{
尝试
{
//执行场景中需要的任何操作。
System.IO.FileInfo fi=新的System.IO.FileInfo(文件);
WriteLine(“{0}:{1},{2}”,fi.Name,fi.Length,fi.CreationTime);
}
catch(System.IO.filenotfound异常)
{
//如果文件被单独的应用程序删除
//或自调用TraverseTree()以来的线程
//那就继续吧。
控制台写入线(e.Message);
继续;
}
}
//将子目录推到堆栈上进行遍历。
//这也可以在移交文件之前完成。
foreach(子目录中的字符串str)
直接推力(str);
}
}
}

我只需修改,以便在您进入要查找的目录后仅获取文件。

我可以通过添加
System.IO.DriveInfo.GetDrives()来获取每个逻辑驱动器的路径以开始搜索并存储结果列表,然后通过

将这些字符串传递到答案中。您是否尝试过使用“用于索引”表示您正在尝试建立自己的索引,或寻找一种方法来查询“Windows搜索”建立的现有索引?请注意,默认情况下,后者不包括整个驱动器。我有,但是我不知道开始搜索的基本目录是什么。例如,我正在寻找一个名为myStuff的文件夹,它可能位于驱动器C:、D:、E:、或F:,用于构建我自己的索引。一旦文件被编入索引,我将把它们作为自动备份和版本监控系统传输到服务器。@Engineer13441如果要搜索多个驱动器,请为每个驱动器调用一次。
public class StackBasedIteration
{
    static void Main(string[] args)
    {
        // Specify the starting folder on the command line, or in  
        // Visual Studio in the Project > Properties > Debug pane.
        TraverseTree(args[0]);

        Console.WriteLine("Press any key");
        Console.ReadKey();
    }

    public static void TraverseTree(string root)
    {
        // Data structure to hold names of subfolders to be 
        // examined for files.
        Stack<string> dirs = new Stack<string>(20);

        if (!System.IO.Directory.Exists(root))
        {
            throw new ArgumentException();
        }
        dirs.Push(root);

        while (dirs.Count > 0)
        {
            string currentDir = dirs.Pop();
            string[] subDirs;
            try
            {
                subDirs = System.IO.Directory.GetDirectories(currentDir);
            }
            // An UnauthorizedAccessException exception will be thrown if we do not have 
            // discovery permission on a folder or file. It may or may not be acceptable  
            // to ignore the exception and continue enumerating the remaining files and  
            // folders. It is also possible (but unlikely) that a DirectoryNotFound exception  
            // will be raised. This will happen if currentDir has been deleted by 
            // another application or thread after our call to Directory.Exists. The  
            // choice of which exceptions to catch depends entirely on the specific task  
            // you are intending to perform and also on how much you know with certainty  
            // about the systems on which this code will run. 
            catch (UnauthorizedAccessException e)
            {                    
                Console.WriteLine(e.Message);
                continue;
            }
            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
                continue;
            }

            string[] files = null;
            try
            {
                files = System.IO.Directory.GetFiles(currentDir);
            }

            catch (UnauthorizedAccessException e)
            {

                Console.WriteLine(e.Message);
                continue;
            }

            catch (System.IO.DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
                continue;
            }
            // Perform the required action on each file here. 
            // Modify this block to perform your required task. 
            foreach (string file in files)
            {
                try
                {
                    // Perform whatever action is required in your scenario.
                    System.IO.FileInfo fi = new System.IO.FileInfo(file);
                    Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
                }
                catch (System.IO.FileNotFoundException e)
                {
                    // If file was deleted by a separate application 
                    //  or thread since the call to TraverseTree() 
                    // then just continue.
                    Console.WriteLine(e.Message);
                    continue;
                }
            }

            // Push the subdirectories onto the stack for traversal. 
            // This could also be done before handing the files. 
            foreach (string str in subDirs)
                dirs.Push(str);
        }
    }
}