2013年3月25日 星期一

[C#] 刪除1天前某個目錄的檔案

            //刪除1天前某個目錄的檔案,減少temp檔佔用空間
            try
            {
                string TmpRootPath= @"C:\temp";
                DirectoryInfo TempInfo = new DirectoryInfo(TmpRootPath);
                FileInfo[] TempArray = TempInfo.GetFiles();
                if (TempArray.Length > 0)
                {
                    for (int i = 0; i < TempArray.Length; i++)
                    {
                        string TempFileName = TempArray[i].Name;
                        DateTime dtTempFileEditTime = TempArray[i].LastWriteTime;
                        TimeSpan ts = DateTime.Now - dtTempFileEditTime;
                        int differenceInDays = ts.Days;
                        if (differenceInDays > 1) File.Delete(TmpRootPath+ TempFileName);
                    }
                }