473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C#: help with coding

Hi All,
I have the following class which deletes files older than X number of days and with specific extension. I dont see any error in the code from my point of you. Please, can anyone double check for me because when i m debuging it, files are not getting deleted. Thanks:)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4.  
  5. namespace Files_ExtLib1
  6. {
  7.     public class FolderWatcher
  8.     {
  9.         string m_FolderPath;
  10.  
  11.         public FolderWatcher()
  12.         {
  13.             //
  14.             // TODO: Add constructor logic here
  15.             //
  16.         }
  17.  
  18.         #region Property
  19.         public string FolderPath
  20.         {
  21.             get
  22.             {
  23.                 return m_FolderPath;
  24.             }
  25.             set
  26.             {
  27.                 m_FolderPath = value;
  28.             }
  29.         }
  30.         #endregion
  31.  
  32.         public int DeleteFilesOlderThan(int Days, int Hrs, int Mins, string filter)
  33.         {
  34.             DateTime dt = GetRelativeDateTime(Days, Hrs, Mins);
  35.             ArrayList oldFilePaths = FilesOlderThan(dt, filter);
  36.             int count = 0;
  37.             foreach (string filePath in oldFilePaths)
  38.             {
  39.                 try
  40.                 {
  41.                     File.Delete(filePath);
  42.                     count++;
  43.                 }
  44.                 catch (Exception e)
  45.                 {
  46.                     Console.WriteLine("Unable to delete " + filePath);
  47.                     Console.WriteLine("Reason: {0} ", e);
  48.                 }
  49.                 Console.WriteLine("Files successfully deleted");
  50.             }
  51.             return count;
  52.         }
  53.  
  54.         // Using GetRelativeDateTime as a private method instead of public however as it is only to be used internally
  55.         private static DateTime GetRelativeDateTime(int days, int hours, int minutes)
  56.         {
  57.             return DateTime.Now.AddDays(-days).AddHours(-hours).AddMinutes(-minutes);
  58.         }
  59.  
  60.         public ArrayList FilesOlderThan(DateTime dt, string filter)
  61.         {
  62.             // Using the supplied path 
  63.             DirectoryInfo directory = new DirectoryInfo(m_FolderPath);
  64.             // Filter is applied to the directory.GetFiles method to get files of given extension
  65.             // get FileInfos for the files in the current directory matching the filter expression
  66.             FileInfo[] files = directory.GetFiles(filter);
  67.             //list to hold the result
  68.             ArrayList older = new ArrayList();  
  69.             foreach (FileInfo file in files)
  70.             {
  71.                 //if smaller (older) than the relative time 
  72.                 if (file.CreationTime < dt) 
  73.                     older.Add(file.Name); //add it to the list 
  74.             }
  75.             return older;
  76.         }
  77.     }
  78. }
  79.  
Entry point
Expand|Select|Wrap|Line Numbers
  1.         private void DeleteFiles_Click(object sender, EventArgs e)
  2.         {
  3.             FolderWatcher fw = new FolderWatcher();
  4.             fw.FolderPath = @"c:\csharp"; // FolderName
  5.             //OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
  6.             fw.DeleteFilesOlderThan(int.Parse(Textbox1.Text), int.Parse(Textbox2.Text), int.Parse(Textbox3.Text), "*" + filetype.SelectedItem.ToString()); // deletes everything older than specified days,hrs, and mins respectively 
  7.             //fw.DeleteFilesOlderThan(0, 0, 0, "*.txt");
  8.         }
  9.  
Dec 10 '07 #1
0 1023

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Larry | last post by:
Help Please http://www.angelfire.com/folk/ps197_brooklyn_ny I currently have a main page that has a list of about 50 names. I then have 50 pages that are linked to the main page The...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
14
by: Jon Maz | last post by:
Hi, I am writing inline code .aspx pages using VS.Net 2002. The color coding seems to work for VB.NET inline, and not for C# inline (see below). Does anyone know how to get this working for...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
7
by: VK | last post by:
In commemoration of 8th Google anniversary my present to Mr.Cornford :-) <http://groups.google.com/group/mozilla.dev.tech.svg/browse_frm/thread/5dabd0138008a9ec/> (useful to read to anyone who...
3
by: editormt | last post by:
A recent poll asked if programming standards are used by development organisations... and if they are controlled. None: 20% Yes, but without control: 49% Yes, with control: 31% Participants:...
8
by: =?ISO-8859-1?Q?Arnaud_Carr=E9?= | last post by:
Hi all, I guess you all know how difficult it is to choose a conding standard. And even more difficult it is to explain the choice to your dev team :-) I'm looking for an "official" c++ coding...
9
by: mekalai82 | last post by:
i have information.php file that file contain following coding <?php echo phpinfo(); ?> while i calling the URL ("http://localhost/information.php"). i am getting the coding <?php echo...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.