如何在文件夹 vb.net 中找到最旧的文件夹 [英] How to find the oldest Folder in a Folder vb.net

查看:22
本文介绍了如何在文件夹 vb.net 中找到最旧的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个备份文件夹的程序.我想说 5 个文件夹,然后如果再次备份它,我希望删除 5 个文件夹中最旧的文件夹,并将新文件夹放入其中.

I am trying to make a program that backs up folders. I want to have say 5 folders, then if it is backed up again I want the oldest of the 5 folders to be deleted and the new one placed in it.

如何在目录中找到最旧的文件夹

How would I find the oldest folder in a directory

推荐答案

使用 System.IO.DirectoryInfo 可能会有所帮助.

Using System.IO.DirectoryInfo could be helpful.

特别是关于 CreationTime 属性和 EnumerateDirectories 方法.

Specifically with regards to the CreationTime property and EnumerateDirectories method.

以下是使用 LINQ 的 EnumerateDirectories 方法的修改示例:

Here is a modified sample for the EnumerateDirectories method using LINQ:

' Create a DirectoryInfo of the Program Files directory.
Dim dirPrograms As New DirectoryInfo("c:\program files")

' LINQ query for oldest directory
Dim dir = (From dir In dirPrograms.EnumerateDirectories()).Min(function (o) o.CreationTime).FirstOrDefault()

If Not IsNothing(dir) Then
' perform rest of function
End If

这是获取文件夹中最旧目录的非 LINQ 版本:

Here is a non LINQ version to get the oldest directory in a folder:

    Dim di As New DirectoryInfo("C:\program files")
    Dim dirs() as DirectoryInfo = di.GetDirectories()
    Dim creationTime as DateTime = DateTime.Now
    Dim oldestDir As DirectoryInfo

    For Each dir As DirectoryInfo In dirs
        If DateTime.Compare(dir.CreationTime(), creationTime) < 0 Then
            oldestDir = dir
            creationTime = dir.CreationTime()
        End If
    Next

这篇关于如何在文件夹 vb.net 中找到最旧的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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