扫描使用C#下钻驱动器? [英] Scanning a drive with drilldowns using C#?

查看:78
本文介绍了扫描使用C#下钻驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个扫描驱动器的应用程序。最棘手的部分虽然是我的驱动器包含了一组具有文件夹中的文件夹和包含文档的文件夹。我试图扫描的驱动器,需要的所有文件和放大器的快照; ,文件夹和转储到一个.txt文件

我第一次运行这个程序,输出将与所有文件夹和放大器的文本文件;文件。

我第二次运行该应用程序,它会采取2文本文件(从第二次我运行应用程序,并从第1次.txt文件产生的一个我已经运行了应用程序),并比较他们...转述已经移到/重写/删除。

I'm trying to create an application which scans a drive. The tricky part though, is that my drive contains a set of folders that have folders within folders and contain documents. I'm trying to scan the drive, take a "snapshot" of all documents & folders and dump into a .txt file.
The first time i run this app, the output will be a text file with all the folders & files.
The second time i run this application, it will take the 2 text files (the one produced from the 2nd time i run the app and the .txt file from the 1st time i have run the app) and compare them...reporting what has been moved/overridden/deleted.

没有任何人有这方面的任何代码?我在此C#的东西一个新手,任何帮助将不胜感激。

Does anybody have any code for this? I'm a newbie at this C# stuff and any help would be greatly appreciated.

先谢谢了。

推荐答案

有一件事情,我们了解到80年代是,如果它真的很有诱惑力的使用文件系统散步递归,但你做的那一刻,有人将与嵌套层次的文件系统,这将导致你的栈溢出。这是迄今为止最好使用文件系统的基于堆的行走。

One thing that we learned in the 80's was that if it's really tempting to use recursion for file system walking, but the moment you do that, someone will make a file system with nesting levels that will cause your stack to overflow. It's far better to use heap-based walking of the file system.

下面是一类我拼凑它做到了这一点。这不是超级漂亮,但它的工作相当不错:

Here is a class I knocked together which does just that. It's not super pretty, but it does the job quite well:

using System;
using System.IO;
using System.Collections.Generic;

namespace DirectoryWalker
{
    public class DirectoryWalker : IEnumerable<string>
    {
        private string _seedPath;
        Func<string, bool> _directoryFilter, _fileFilter;

        public DirectoryWalker(string seedPath) : this(seedPath, null, null)
        {
        }

        public DirectoryWalker(string seedPath, Func<string, bool> directoryFilter, Func<string, bool> fileFilter)
        {
            if (seedPath == null)
                throw new ArgumentNullException(seedPath);
            _seedPath = seedPath;
            _directoryFilter = directoryFilter;
            _fileFilter = fileFilter;
        }

        public IEnumerator<string> GetEnumerator()
        {
            Queue<string> directories = new Queue<string>();
            directories.Enqueue(_seedPath);
            Queue<string> files = new Queue<string>();
            while (files.Count > 0 || directories.Count > 0)
            {
                if (files.Count > 0)
                {
                    yield return files.Dequeue();
                }

                if (directories.Count > 0)
                {
                    string dir = directories.Dequeue();
                    string[] newDirectories = Directory.GetDirectories(dir);
                    string[] newFiles = Directory.GetFiles(dir);
                    foreach (string path in newDirectories)
                    {
                        if (_directoryFilter == null || _directoryFilter(path))
                            directories.Enqueue(path);
                    }
                    foreach (string path in newFiles)
                    {
                        if (_fileFilter == null || _fileFilter(path))
                            files.Enqueue(path);
                    }
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}



典型用法是这样的:

Typical usage is this:

DirectoryWalker walker = new DirectoryWalker(@"C:\pathToSource\src", null, (x => x.EndsWith(".cs")));
foreach (string s in walker)
{
    Console.WriteLine(s);
}



这递归列出了的.cs

Which recursively lists all files that end in ".cs"

这篇关于扫描使用C#下钻驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆