2 Replies - 19106 Views - Last Post: 16 March 2008 - 06:55 AM Rate Topic: -----

#1 blue1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 08

compare 2 dates and if more than 7 days delete file.

Post icon  Posted 13 March 2008 - 02:24 AM

hi,
I am doing a program with c# and mysql database.
the task is actually taking 2 dates out from the mysql database and compare the 2 dates. if it is more than x days den the file will be deleted.
the following is the program that i have done but i am stuck at the logic that is compare the 2 days if more than x days delete the file.
string ConnectionString = "server=localhost;user id=name;password;persist security info=True;database=name of database;";
				using (MySqlConnection conn = new MySqlConnection(ConnectionString))
				{
					MySqlCommand cmd = new MySqlCommand();
					conn.Open();
					if (conn.State == ConnectionState.Open)
					{


						cmd.Connection = conn;
						cmd.CommandText = "select history ,lastclrhistorydate from table;";
						cmd.CommandType = CommandType.Text;
						MySqlDataReader reader;
						reader = cmd.ExecuteReader();
						if ((reader != null))
						{
							DataTable dt = new DataTable();
							dt.Load(reader);
							
							if (dt.Rows.Count == 1)
							{
								int history = int.Parse(dt.Columns[0].ToString());
								
								string lastclrhistory =dt.Columns[1].ToString();
								DateTime currentdate = DateTime.Now;
								
								
								if ((currentdate - lastclrhistory) == history)
								{
									//(File.delete(file)
								}


thank you

Is This A Good Question/Topic? 0
  • +

Replies To: compare 2 dates and if more than 7 days delete file.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: compare 2 dates and if more than 7 days delete file.

Posted 13 March 2008 - 05:00 AM

To get the number of days between 2 dates I would use the Timespan Class, here's an example:


/********************************************
         This is for example only. Here I will be using the
         Timespan class to calculate the # of days
         between 2 dates
********************************************/
//get the current date
DateTime nowDate = System.DateTime.Now();
//get my 2nd date from the database
DateTime thenDate = (DateTime)dt.Columns[1].ToString();
//use timespan to get the number of days
System.Timespan span = nowDate - thenDate
int days = (int)span.TotalDays

//now check to see if it's been more than 7 days
if(days > 7)
{
    //now delete your file in here
}




Hope that helps :)
Was This Post Helpful? 1

#3 davegeek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 30-January 08

Re: compare 2 dates and if more than 7 days delete file.

Posted 16 March 2008 - 06:55 AM

Here are my 2 cents:
1. Where is the file located? If on the hard drive then u need to use something like this:
FileInfo aFileInfo = new FileInfo("Data.txt");
FileStream aFile = aFileInfo.OpenRead();
2. Make sure that the connections to the file are closed, otherwise the Windows won't let u delete it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1