Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Deleting temporary files from a folder

Deleting temporary files from a folder

Scheduled Pinned Locked Moved Clever Code
helpcsharpdatabasesysadminbusiness
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xiangyang Liu
    wrote on last edited by
    #1

    My web application needs to write files in a temp folder. The files have to be deleted later, otherwise the disk on the server will be filled up. So I wrote the following code to be executed in a background thread periodically: // delete files more than 1 day old DateTime now = DateTime.Now; string[] pFiles = Directory.GetFiles(sTempFolder,"*.*"); for(int i=0;i<pFiles.Length;i++) {   try   {     if(File.GetCreationTime(pFiles[i]).AddDays(1)<now)     {       File.Delete(pFiles[i]);     }   }   catch(Exception oBug) { /* error handling */ } } Then I copied some old files (much older than 1 day) into the temp folder and try to test the code. It does not work. It turns out, it would have worked if I waited for a day after copying the files. The "creation time" for the old files I copied was set to the current time, the explorer shows the "last modification time". If I change GetCreationTime to GetLastWriteTime, then it will work right away.

    My .NET Business Application Framework My Home Page

    M 1 Reply Last reply
    0
    • X Xiangyang Liu

      My web application needs to write files in a temp folder. The files have to be deleted later, otherwise the disk on the server will be filled up. So I wrote the following code to be executed in a background thread periodically: // delete files more than 1 day old DateTime now = DateTime.Now; string[] pFiles = Directory.GetFiles(sTempFolder,"*.*"); for(int i=0;i<pFiles.Length;i++) {   try   {     if(File.GetCreationTime(pFiles[i]).AddDays(1)<now)     {       File.Delete(pFiles[i]);     }   }   catch(Exception oBug) { /* error handling */ } } Then I copied some old files (much older than 1 day) into the temp folder and try to test the code. It does not work. It turns out, it would have worked if I waited for a day after copying the files. The "creation time" for the old files I copied was set to the current time, the explorer shows the "last modification time". If I change GetCreationTime to GetLastWriteTime, then it will work right away.

      My .NET Business Application Framework My Home Page

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi! Leaving temp files on the disk for longer than absolutely neccessary is a potential security risk. Shouldn't you remove the temp files as soon as your app doesn't need them any more instead of deleting files of a certain age?

      Regards, mav -- Black holes are the places where God divided by 0...

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups