使用C#使用向下扫描驱动器? [英] Scanning a drive with drilldowns using C#?
问题描述
我正在尝试创建一个扫描驱动器的应用程序。但棘手的部分是,我的驱动器包含一组文件夹中的文件夹和包含文档的文件夹。我试图扫描驱动器,采取快照的所有文件&文件夹并转储到.txt文件。
第一次运行此应用程序时,输出将是一个文本文件,所有文件夹&文件。
第二次运行这个应用程序,它将需要2个文本文件(从第二次运行应用程序和.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();
}
}
}
/ p>
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屋!