0

我正在尝试在具有大量数据的指定目录中计算上次写入时间早于 10 分钟的所有文件的总大小。这是我在下面尝试过的代码:

DateTime from_date = DateTime.Now.AddMinutes(-10);
List<string> list = Directory.GetFiles(@[PATH], "*", SearchOption.AllDirectories)               
            .Where(x => File.GetLastWriteTime(x).Date > from_date)
            .Select(x => new FileInfo(x).FullName)
            .ToList();            

foreach (String s in list)
{
    FileInfo file = new FileInfo(s);
    size += file.Length;
}

button6.Text = ((size / 1024f) / 1024f) + "MB";

这在我指定本地路径时有效,但在指定网络路径时抛出网络名称不再可用IOException。为什么会这样?有没有一种方法可以让我“保持”网络路径或确保与该路径的连接不会失败?

4

1 回答 1

0

DirectoryInfo.EnumerateFiles()并且DirectoryInfo.EnumerateDirectories是最好的选择。

var directories = new DirectoryInfo(root).EnumerateDirectories().OrderByDescending(d => d.Name);
IEnumerable<FileInfo> files;

foreach (DirectoryInfo directoryInfo in directories)
{
    files = directoryInfo
        .EnumerateFiles("*" + extension, SearchOption.AllDirectories)
        .OrderByDescending(f => f.LastWriteTime);
    foreach (FileInfo f in files)
    {
        if ((f.LastWriteTime.CompareTo(DateTime.Now.AddMinutes(-10)) == 1)
        {
            // Do whatever you want with the files
        }
    }
}
于 2013-12-12T15:58:09.933 回答