Results 1 to 2 of 2

Thread: RESOLVED delete a file older than thirty days I am getting a directory Invalid error

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    790

    Resolved RESOLVED delete a file older than thirty days I am getting a directory Invalid error

    I placed this code yesterday which focuses on deleting a file older the thirty days and this process checks after my streamwriter code the whole thing looks like this :

    Code:
     public LoggingAction( GlobalConfiguration globalConfig, PayFilePackageCol paymentfilePackCol)
    
            {
                GlobalConfig = globalConfig;
                PayFilePackCol = paymentfilePackCol;
                logfilePath1 = this.GlobalConfig.Payment_Log_File_Loc;
    
    
          
    
    
        
    
    
    
                String folder = Path.GetDirectoryName(logfilePath1);
                String fileName = Path.GetFileNameWithoutExtension(logfilePath1);
                String extension = Path.GetExtension(logfilePath1);
    
                String newName = folder + "\\" + fileName + "_" + ISO_Date() + extension;
    
             
    
                pSW = new StreamWriter(newName, true);
    
    
    
                string[] files = Directory.GetFiles(newName);
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    if (fi.LastAccessTime < DateTime.Now.AddDays(-30))
    
                        fi.Delete();
                } 
    
    
    
    
    
    
            }
    
    
            ~LoggingAction()
            {
    now the global config. Payment_Log_File_Loc is acutally C:\Temp on my local c drive so unless my permissions are set that I can't delete from there what else could be going on to fire off that exception ?
    Last edited by Christopher_Arm; Oct 7th, 2011 at 01:49 PM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    790

    Re: deleting a file older than thirty days I am getting a directory Invalid error

    Solved this myself later today. After lunch

    Code:
        string[] files = Directory.GetFiles(folder);
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    if (fi.LastWriteTime < DateTime.Now.Subtract(TimeSpan.FromDays(30)))
                    
                        fi.Delete();
                }
    The last write time is what needs to be accessed in the file not the creation or accessed time wihich will usually be in sync date time now whenever the file is tampered with, moved etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width