.Net 4.5 Winforms处理超过X天的任何内容
所以,我有一个很棒的小应用程序,它将递归地搜索一个目录,查找给定的文件扩展名。。。压缩找到的文件,然后删除原始文件。。。除了最新的文件 我想知道的是,如何合并一种方法来处理X天数的文件 例如,假设我有一个日志目录,其中有365个*.log文件。现在,我的代码将压缩并删除除最新代码以外的所有代码。。。我想在中添加一个选项来执行任何超过X天的操作。。。说任何超过30天的话 以下是我现在执行此工作的现有代码:.Net 4.5 Winforms处理超过X天的任何内容,.net,vb.net,winforms,.net-4.5,.net,Vb.net,Winforms,.net 4.5,所以,我有一个很棒的小应用程序,它将递归地搜索一个目录,查找给定的文件扩展名。。。压缩找到的文件,然后删除原始文件。。。除了最新的文件 我想知道的是,如何合并一种方法来处理X天数的文件 例如,假设我有一个日志目录,其中有365个*.log文件。现在,我的代码将压缩并删除除最新代码以外的所有代码。。。我想在中添加一个选项来执行任何超过X天的操作。。。说任何超过30天的话 以下是我现在执行此工作的现有代码: Imports System.ComponentModel Imports System.I
Imports System.ComponentModel
Imports System.IO
Imports System.IO.Compression
Public Class DoTheWork
Public _ct As Long = 0
Public Function Work(ByVal _PathToSearch As String, ByVal _Ext As String, ByVal _Worker As BackgroundWorker) As Boolean
Try
Dim _DI As New DirectoryInfo(_PathToSearch)
Dim _Files = _DI.EnumerateFiles("*." & _Ext, SearchOption.AllDirectories).ToList
Dim _fCt As Long = _Files.Count
For i = 0 To _fCt - 1
Dim _Path As String = _Files(i).DirectoryName
Dim _FullName As String = _Files(i).FullName
If Not GetNewestFile(_Path).FullName.Equals(_FullName) Then
Dim _FileName As String = Path.GetFileNameWithoutExtension(_Files(i).Name)
Using _z As ZipArchive = ZipFile.Open(_Path & "\" & _FileName & ".zip", ZipArchiveMode.Create)
_z.CreateEntryFromFile(_FullName, _Files(i).Name, CompressionLevel.Optimal)
End Using
File.Delete(_FullName)
End If
ReportProgress(_Perc(i, _fCt), _Worker)
Next
ReportProgress(100, _Worker)
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub ReportProgress(ByVal ct As Integer, _Worker As BackgroundWorker)
If _Worker IsNot Nothing Then
_Worker.ReportProgress(ct)
Threading.Thread.Sleep(250)
End If
End Sub
Private Function _Perc(num, ct) As Integer
If num = 0 Then
Return 1
Else
Return (num / ct) * 100
End If
End Function
Private Function GetNewestFile(_path As String) As FileInfo
Dim di As New DirectoryInfo(_path)
Return (di.GetFiles("*.bak").OrderByDescending(Function(f) f.LastWriteTime).First())
End Function
End Class
更新:
.Where语句完全忽略了_ToKeep听起来像是我曾经为.Net C缓存编写的一些代码:
public void CullCache()
{
var now = new DateTime().Date;
now = now.AddDays(- (Math.Max(0, KeepDays - 1)) );
var rootDir = new DirectoryInfo(LocationPathRoot);
RecurseDirs(now, rootDir);
}
// Delete logs even if in sub directories.
private void RecurseDirs(DateTime now, DirectoryInfo root)
{
FileInfo[] files = null;
try
{
// get all the files directly under this folder
files = root.GetFiles("*.*");
}
catch (UnauthorizedAccessException)
{
// Thrown if the files require permissions greater than the application provides.
// just ignore for now
}
catch (System.IO.DirectoryNotFoundException)
{
// just ignore for now
}
if (files != null)
{
// delete files created more than "KeepDays" ago...
foreach (FileInfo fi in files.Where(fi => fi.CreationTimeUtc.CompareTo(now) < 0).Where(fi => FindCacheFile(fi.FullName)))
{
Console.WriteLine("Delete {0}", fi.FullName);
}
foreach (var dirInfo in root.GetDirectories())
{
RecurseDirs(now, dirInfo);
}
}
}
您可能需要更改GetFiles。要匹配要删除的文件。这是一个带有Linq的单行程序,请使用Select和Where扩展名方法。它只有一行和一个小茄子。。。。如果是,那是什么?仅供参考:您自己创建所有日志文件,所以应该可以,但文件创建和修改时间不可靠。复制文件时,时间会改变;移动文件时,时间不会改变。即便如此,仍有一些特定案例违反了这些规则。除非您创建了这些文件,并且从未从其原始位置复制它们,否则您最多只能猜测。这些文件将由系统生成,除非需要恢复,否则几乎不会被触及。Where子句将首选布尔值,但您将从中为其指定一个整数。我怀疑如果你在那里输入一个<0,它会起作用。为了澄清,我可以将GetNewestFile_path更改为String,将GetNewestFile_path更改为String,将Byval _daystokep更改为Integer,并将查询更改为.Where?因此,我更改了代码以完成此操作,但它完全忽略了_tokep。我已经用新的工作函数更新了这个问题
public void CullCache()
{
var now = new DateTime().Date;
now = now.AddDays(- (Math.Max(0, KeepDays - 1)) );
var rootDir = new DirectoryInfo(LocationPathRoot);
RecurseDirs(now, rootDir);
}
// Delete logs even if in sub directories.
private void RecurseDirs(DateTime now, DirectoryInfo root)
{
FileInfo[] files = null;
try
{
// get all the files directly under this folder
files = root.GetFiles("*.*");
}
catch (UnauthorizedAccessException)
{
// Thrown if the files require permissions greater than the application provides.
// just ignore for now
}
catch (System.IO.DirectoryNotFoundException)
{
// just ignore for now
}
if (files != null)
{
// delete files created more than "KeepDays" ago...
foreach (FileInfo fi in files.Where(fi => fi.CreationTimeUtc.CompareTo(now) < 0).Where(fi => FindCacheFile(fi.FullName)))
{
Console.WriteLine("Delete {0}", fi.FullName);
}
foreach (var dirInfo in root.GetDirectories())
{
RecurseDirs(now, dirInfo);
}
}
}