单击按钮时记录删除的文件?记录所有文件(删除和未删除)

问题描述:

我正在编写一个应用程序来删除超过6个月的测试文件夹中的文件,应用程序正常工作,因为我测试了它,我想创建一个日志文件以跟踪用于审计目的的已删除文件的名称。单击按钮时记录删除的文件?记录所有文件(删除和未删除)

但是下面的scirpt确实记录了所有文件(删除和未删除),我需要的只是记录日期和时间以及删除文件的名称。

谢谢

下面的脚本:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace Delete_PDF_Files 
{ 
    public partial class Form1 : Form 
    { 
     private string strLogText; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnCheck_Click(object sender, EventArgs e) 
     { 
      // check the number of file in the CPS directory on S drive 
      listBox1.Items.Clear(); 

      string[] files = System.IO.Directory.GetFiles(@"C:\test\"); // @"S:\CPS Papers\" 
      this.listBox1.Items.AddRange(files); 
      textBox1.Text = listBox1.Items.Count.ToString(); 
     } 

     // delete button to delete files over 6 months from CPS folder 
     private void btnDelete_Click(object sender, EventArgs e) 
     { 
      string[] files = System.IO.Directory.GetFiles(@"C:\test\"); //S:\CPS Papers test C:\test\ 

      foreach (string file in files) 
      { 
       System.IO.FileInfo fi = new System.IO.FileInfo(file); 

       if (fi.LastWriteTime < DateTime.Now.AddMonths(-6)) 
        fi.Delete(); 

       // Create a writer and open the file: //C:\test\log 
       System.IO.StreamWriter log; 

       if (!System.IO.File.Exists("C:\\test\\log\\logfile.txt")) 
       { 
        log = new System.IO.StreamWriter("C:\\test\\log\\logfile.txt"); 
       } 
       else 
       { 
        log = File.AppendText("C:\\test\\log\\logfile.txt"); 
       } 

       // Write to the file: 
       log.WriteLine(DateTime.Now); 
       log.WriteLine(strLogText); 
       log.WriteLine(); 
       log.WriteLine(); 

       // Close the stream: 
       log.Close(); 

      } 
     } 

     // Exit button 
     private void btnExit_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 
    } 
} 
+0

或者你可以只使用PowerShell单行.... – 2013-04-20 09:25:19

替换删除代码这一个:

private void btnDelete_Click(object sender, EventArgs e) 
    { 
     string[] files = System.IO.Directory.GetFiles(@"C:\test\"); //S:\CPS Papers test C:\test\ 

     foreach (string file in files) 
     { 
      System.IO.FileInfo fi = new System.IO.FileInfo(file); 
      //if (fi.LastAccessTime < DateTime.Now.AddMonths(-3)) 
      if (fi.LastWriteTime < DateTime.Now.AddMonths(-6)) 
      { 
       fi.Delete(); 
       using (StreamWriter writer = File.AppendText("C:\\test\\log\\logfile.txt")) 
       { 
        writer.Write("File: " + file + " deleted at : "+DateTime.Now); 
        writer.WriteLine("----------------------------------------------------"); 
        writer.Flush(); 
        writer.Close(); 

       } 
      } 

     } 
    } 
+0

您好,感谢您的快速回复,我刚才试了一下它的辉煌记录,它仍然显示未被删除的文件,因为它们不到6个月,并将它们显示为已删除文件:C:\ test \ Plumbing Covering Letter.docx删除:20/04/2013 10:35:37文件:C: \ test \ algeràune certaineépoque.docxdeleted at:20/04/2013 10:35:37 --------------------------- ------------------------- File:C:\ test \ cover letter.docx deleted at:20/04/2013 10:35:37 --- – user2038015 2013-04-20 09:39:37

+0

这似乎是不可能的,因为代码在你的条件内'fi.LastWriteTime 2013-04-20 10:25:23

而不是使用自定义日志记录的,你在做我会建议你使用一个好的图书馆,如Log4Net。为什么重新发明*?我知道它的学习曲线时间很短,但一旦你了解了你,你可以很容易地将它整合到任何新的项目中。

只需在你的app.config中添加一个配置部分,并给出here和几行代码,就可以开始了。

上log4net的一个很好的教程,可以发现here

+0

谢谢,稍后再检查一下,我在网上看到了很少的Log4Net的推荐。 – user2038015 2013-04-20 09:46:21

+0

太棒了!我一直在为此工作六个月,但仍希望我早些时候开始使用它。 – 2013-04-20 09:49:52