[英]Log deleted files when button is clicked? Log all files (deleted and undeleted)
I am writing an application to delete file on a test folder that are over 6 months, the application works fine as I have tested it, I wanted to create a log file to keep track of the name of the deleted files for audit purpose. 我正在编写一个应用程序,以删除超过6个月的测试文件夹中的文件,该应用程序在测试后可以正常运行,我想创建一个日志文件来跟踪已删除文件的名称,以进行审核。
but with the scirpt below it does record all the files (deleted and undeleted), all I need is just record the date and time and the name of the deleted files. 但是下面的脚本确实记录了所有文件(已删除和未删除),我只需要记录日期和时间以及已删除文件的名称。
Thank you 谢谢
Script Below: 以下脚本:
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();
}
}
}
Replace you delete code with this one: 用此代码替换您的删除代码:
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();
}
}
}
}
Instead of using custom logging as you are doing I would recommend that you use a good library like Log4Net . 建议您不要使用类似Log4Net的良好库,而不要像这样做那样使用自定义日志记录。 Why reinvent the wheel?
为什么要重新发明轮子? I know that it has a small learning curve time but once you get to know you you can easily integrate it in any new projects.
我知道它的学习曲线时间很短,但是一旦您了解了就可以轻松地将其集成到任何新项目中。
By just adding a config section to your app.config as given here and a few lines of code you should be ready to go. 只需在此处给app.config添加一个config部分,并添加几行代码,就可以开始了。
A good tutorial on Log4Net can be found here 在这里可以找到有关Log4Net的很好的教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.