简体   繁体   English

如何重复生成同名的output文件

[英]How to repeatedly generate output file with same name

I've a service that generates multiple output files for every iteration run.我有一个服务,它为每次迭代运行生成多个 output 文件。 It runs one time a day.它每天运行一次。 The output filename is always constant and the older file is deleted and then replaced by a new one. output 文件名始终不变,旧文件被删除,然后被新文件替换。 For eg:例如:

Output files generated for todays iteration will be为今天的迭代生成的 Output 文件将是

  • abc.txt abc.txt
  • def.txt def.txt
  • ghi.txt ghi.txt

Output files generate for tomorows iteration will delete the older files and the create new files Output 文件为明天生成,迭代将删除旧文件并创建新文件

  • abc.txt abc.txt
  • def.txt def.txt
  • ghi.txt ghi.txt

I'm using the following function in order to achieve what I said.我正在使用以下 function 来实现我所说的。

public static void CreateOutputFile(string Message, string fileName)
{
    try
    {
        string filePath = ConfigurationManager.AppSettings["DESTDIRPATH"] + Path.DirectorySeparatorChar + fileName;
        if (File.Exists(filePath)) //Check if file exists if yes then compare last modified date and delete the older file
        {
            DateTime dateTime = File.GetLastWriteTime(filePath);
            if (dateTime < System.DateTime.Now.AddHours(-5))
            {
                File.Delete(filePath);
            }
        }
        
        if (!File.Exists(filePath)) //Check if file exists if yes then append else create a new file.
        {
            using (StreamWriter sw = File.CreateText(filePath))
            {
                sw.WriteLine(Message);
            }
        }
        else
        {
            using (StreamWriter sw = File.AppendText(filePath))
            {
                sw.WriteLine(Message);
            }
        }
    }
    catch (Exception ex)
    {
        WriteToFile(ex.Message);
    }
}

This works fine but I'm sure there must be better/optimised/improved code for the above snippet.这很好用,但我确信上面的代码片段必须有更好/优化/改进的代码。 So please let me know your suggestions for the same.所以请让我知道你的建议。

Thank you.谢谢你。

One suggestion is to replace the IF block that contains the calls to File.CreateText and FileAppendText with a single call to StreamWriter.一个建议是将包含对 File.CreateText 和 FileAppendText 的调用的 IF 块替换为对 StreamWriter 的一次调用。 Both methods are wrapper methods that call StreamWriter.这两种方法都是调用 StreamWriter 的包装器方法。 The only different is what they pass in for the append flag.唯一不同的是它们为 append 标志传递的内容。 In your case the value will be true for append if file exist or otherwise create the file.在您的情况下,如果文件存在或以其他方式创建文件,则 append 的值将为真。

Example 1:示例 1:

        //Check if file exists if yes then compare last modified date and delete the older file
        if (File.Exists(filePath))
        {
            DateTime dateTime = File.GetLastWriteTime(filePath);
            if (dateTime < System.DateTime.Now.AddHours(-5))
            {
                File.Delete(filePath);
            }
        }

        using (StreamWriter sw = new StreamWriter(filePath,true))
            sw.WriteLine(Message);

Example 2: Utilizing additional file access options:示例 2:使用其他文件访问选项:

        using (FileStream fs = new FileStream(filePath,             // File full path
                                              FileMode.Append,      // Open for append or create
                                              FileAccess.Write,     // Write mode only
                                              FileShare.ReadWrite)) // Open for Shared Read/Write access
        using (StreamWriter sw = new StreamWriter(fs))
            sw.WriteLine(Message);

See File.cs for File.CreaeText and File.AppendText source code implementation.有关File.CreaeText和 File.AppendText 源代码实现,请参见 File.cs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM