如何在测试文件中写入文件和文件名的计数,这些文件和文件名用c sharp清除 [英] how do I write the count of files and file names in a Test file those are purged in c sharp

查看:56
本文介绍了如何在测试文件中写入文件和文件名的计数,这些文件和文件名用c sharp清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我写了一个脚本,其中30天之前的所有文件都将被清除或删除。但我想知道有多少文件和文件名 -

1.删除的文件数

2.文件名



我已准备好以下脚本。你能帮我解决一下如何在脚本中包含这两个条件 -



foreach(Directory.EnumerateFiles(DirNameSIPOS2)中的字符串fileSIPOS2)

{



FileInfo fiSIPOS2 =新FileInfo(fileSIPOS2);

if(fiSIPOS2.LastWriteTime< DateTime.Now.AddDays(-DaytoDelete) ))

{

fiSIPOS2.Delete();

}



在此先感谢

解决方案

您可以使用任何类型的数组或通用列表对象来跟踪要删除的文件。这是一种实现此目的的简单方法。



 HashSet< string> deletedFiles =  new  HashSet< string>(); 

foreach string fileSIPOS2 in Directory.EnumerateFiles(DirNameSIPOS2))
{
FileInfo fiSIPOS2 = new FileInfo(fileSIPOS2);

if (fiSIPOS2.LastWriteTime < DateTime.Now.AddDays( - DaytoDelete))
{
deletedFiles.Add(fiSIPOS2.FullName);
fiSIPOS2.Delete();
}
}

Console.WriteLine(deletedFiles.Count);

foreach var 条目 in deletedFiles)
{
Console.WriteLine(entry);
}
< / string > < / string > ;


Hi ,
I have written a script in which all the files before 30 days will be Purged or deleted. But I want to know how many files are there and the file names-
1. Count of files deleted
2. Name of the files

I have the below Script already prepared . Can you help me how can I include these two conditions in the script-

foreach (string fileSIPOS2 in Directory.EnumerateFiles(DirNameSIPOS2))
{

FileInfo fiSIPOS2 = new FileInfo(fileSIPOS2);
if (fiSIPOS2.LastWriteTime < DateTime.Now.AddDays(-DaytoDelete))
{
fiSIPOS2.Delete();
}

Thanks in advance

解决方案

You can use any type of array or generic list object to track files you are removing. Here is a simple way to achieve this.

HashSet<string> deletedFiles = new HashSet<string>();

foreach (string fileSIPOS2 in Directory.EnumerateFiles(DirNameSIPOS2))
{
    FileInfo fiSIPOS2 = new FileInfo(fileSIPOS2);
    
    if (fiSIPOS2.LastWriteTime < DateTime.Now.AddDays(-DaytoDelete))
    {
        deletedFiles.Add(fiSIPOS2.FullName);
        fiSIPOS2.Delete();
    }
}

Console.WriteLine(deletedFiles.Count);

foreach(var entry in deletedFiles)
{
    Console.WriteLine(entry);
}
</string></string>


这篇关于如何在测试文件中写入文件和文件名的计数,这些文件和文件名用c sharp清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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