2014-09-02 58 views
1

我在我的SSIS包中编写了一个脚本任务,以便删除包中使用的日志文件,如果它早于X天数。这里是我当前的代码:删除然后重新创建x天以前的文件c#保留createdDate

if (File.Exists((String)Dts.Variables["ErrorLogLocation"].Value)) 
     { 

      DateTime logCreatedDate = File.GetCreationTime((String)Dts.Variables["ErrorLogLocation"].Value); 

      if (logCreatedDate < DateTime.Now.AddDays(-3)) 
      { 
       try 
       { 
        File.Delete((String)Dts.Variables["ErrorLogLocation"].Value); 
       } 
       catch (Exception e) 
       { 
        using (StreamWriter sw = File.AppendText((String)Dts.Variables["ErrorLogLocation"].Value)) 
        { 
         sw.WriteLine(DateTime.Now + " : "+ e); 
        } 
       } 

       using (StreamWriter sw = File.AppendText((String)Dts.Variables["ErrorLogLocation"].Value)) 
       { 
        sw.WriteLine("New log Creation Date: " + File.GetCreationTime((String)Dts.Variables["ErrorLogLocation"].Value)); 
       } 

      } 
     } 

好像它的工作,但问题是,当我删除的文件,然后写入到具有相同名称的新文件,将内部信息被预期擦拭,但文件创建日期与删除之前保持一致。这是一个问题,因为我基于何时在该日期时间删除此文件。在我看来,预期的行为是删除文件,然后用新的创建日期写入一个全新的文件。

任何想法可能会导致此?这是由于我删除一个文件,然后立即附加到一个具有相同名称的文件(我假设它会创建一个新文件,如果它不存在?),这个文件仍然保存在内存中期?

回答

1

documentation

NTFS格式的驱动器可以高速缓存有关文件的信息,如文件 创建时间,对很短的时间周期。因此,如果您是 覆盖或替换现有文件,则可能需要 以显式设置文件的创建时间。

基于此信息,您必须致电File.SetCreationTime以确保它获得所需的时间戳。

+0

我想它是缓存的东西。谢谢! – Dingus 2014-09-02 17:03:20