Streamwriter 未列出已删除的文件夹

Streamwriter not listing the deleted folders

streamwriter 的主要目标(在下面的代码中)应该只报告被删除的目录。如果目录早于设置日期,则目录将被删除。然而,streamwriter 只写最后一个目录而不是删除的项目。我很新,试图了解我做错了什么,感谢您的帮助。

计划

using System;
using System.IO;
using System.Configuration;
using System.Text;

public class Program
{


    static void Main(string[] args)
    {
        //number of days from App.config
        String daysPast = ConfigurationManager.AppSettings["TimeSpan"];

        String path = ConfigurationManager.AppSettings["FilePath"];

        // Reference to a directory.
        DirectoryInfo di = new DirectoryInfo(path);

        // Reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();


        //StreamWriter file name
        string fileName = "fileResults_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm") + ".txt";

        try
        {
            foreach (DirectoryInfo dir in diArr)
            {
                if (dir.LastWriteTime < DateTime.Parse(daysPast))
                    Directory.Delete(dir.FullName, true);


                using (StreamWriter sw = new StreamWriter(fileName, false))
                {
                    foreach (DirectoryInfo FilePath in di.GetDirectories())
                    {
                        sw.WriteLine("File(s) deleted on: " + DateTime.Now);
                        sw.WriteLine("============================================");
                        sw.WriteLine("");
                        sw.WriteLine(dir.FullName,false);
                        sw.WriteLine("");
                        sw.WriteLine("End of list");
                        sw.WriteLine("");
                        sw.Flush();
                    }
                }
            }
        }

        catch (InvalidCastException)
        {
            Console.WriteLine("There was an issue, please try again.");
        }
    }
}

App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="FilePath" value="C:\testFolder\" /><!--root folder -->
    <add key="TimeSpan" value="-2.00:00:00"/><!--adjust days  -->
  </appSettings>
</configuration>

每次使用 foreach (DirectoryInfo dir in diArr) 进行迭代时,您都在重写文件。

此外,如果 if (dir.LastWriteTime < DateTime.Parse(daysPast)) 块没有花括号,只有下一行代码 (Directory.Delete(dir.FullName, true);) 将依赖于该条件,因此 using (StreamWriter sw = new StreamWriter(fileName, false)) 将被执行对于每个目录,因此它总是只写最后一个文件。

您是否需要这样的东西(在 try 块内):

List<string> deletedDirectories = new List<string>();

foreach (DirectoryInfo dir in diArr)
{
    if (dir.LastWriteTime < DateTime.Parse(daysPast))
    {
        Directory.Delete(dir.FullName, true);
        deletedDirectories.Add(dir.FullName);
    }
}

using (StreamWriter sw = new StreamWriter(fileName, false))
{
    sw.WriteLine("File(s) deleted on: " + DateTime.Now);
    sw.WriteLine("============================================");
    sw.WriteLine("");

    foreach (string deletedDirectory in deletedDirectories)
    {
        sw.WriteLine(deletedDirectory);
    }

    sw.WriteLine("");
    sw.WriteLine("End of list");
    sw.WriteLine("");
}

这会将您删除的目录存储在列表中,然后在删除循环完成后将它们写入文件。