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

标签 c#

我正在编写一个应用程序来删除测试文件夹中超过 6 个月的文件,该应用程序在我测试时运行良好,我想创建一个日志文件来跟踪已删除文件的名称以供审核目的。

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

谢谢

脚本如下:

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();
        }
    }
}

最佳答案

用这个替换你删除的代码:

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();

                }
            }

        }
    }

关于c# - 单击按钮时记录删除的文件?记录所有文件(已删除和未删除),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16118694/

相关文章:

c# - 使用 AutoMapper 自定义映射

c# - 移动纸板

c# - RedirectToAction 到不同的 Controller 而不改变 URL

c# - 将结构从 C# 编码到 C++ DLL

c# - 在 C# 中使用字典时出错

c# - 我无法使用 MemoryStream 将 C++ memcpy 代码转换为 C#

c# - 有没有办法通过代码自动打开或关闭 BizTalk 接收位置?

c# - 拖动 WPF Popup 控件

c# - UWP 应用程序无法启动

c# - 使用混合语言(LTR + RTL)重新排列字符串?