簡體   English   中英

單擊按鈕時記錄刪除的文件? 記錄所有文件(已刪除和取消刪除)

[英]Log deleted files when button is clicked? Log all files (deleted and undeleted)

我正在編寫一個應用程序,以刪除超過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();

                }
            }

        }
    }

建議您不要使用類似Log4Net的良好庫,而不要像這樣做那樣使用自定義日志記錄。 為什么要重新發明輪子? 我知道它的學習曲線時間很短,但是一旦您了解了就可以輕松地將其集成到任何新項目中。

只需在此處給app.config添加一個config部分,並添加幾行代碼,就可以開始了。

這里可以找到有關Log4Net的很好的教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM