Click here to Skip to main content
15,883,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
scenario is there is a drive and in that drive there is a files which is automatically created in every 4 mints so i fetch this date time in in this code

now i want to insert data in db that when any file not created within 4 mint then i want to insert data in db

data is sent to db but problem is this data not send when 4 mint is over and i want this condition

What I have tried:

code
C#
try
               {
                   string abc= "";
                   abc= ConfigurationManager.AppSettings.Get("abc");


                   DateTime dattme= File.GetLastAccessTime(abc);


                   Console.WriteLine("the last access time for  c {0}", dattme);

                   if(DateTime.Now.Minute > 4)
                   {
                       DataClasses1DataContext db = new DataClasses1DataContext();
                       tbl_OutBox tb = new tbl_OutBox();
                       tb.ToSIM_No = "+234234324";
                       tb.ToText = "Check please";
                       tb.FromSIM_No = "+234234324";
                       tb.FromText = "Check please";
                       db.tbl_OutBoxes.InsertOnSubmit(tb);
                       db.SubmitChanges();

                   }





               }
               catch (Exception e)
               {
                   Console.WriteLine("The process failed: {0}", e.ToString());
               }
               Thread.Sleep(5000);
           }
       }

how to solve
Posted
Updated 11-May-16 0:58am
v2

I think you have forgotten the little detail of subtracting two times from each other.
Right now you are just taking an arbitrary time and check the Minute part.

Try
C#
TimeSpan timeDiff = DateTime.Now - File.GetLastAccessTime(abc);
if (timeDiff.TotalMinutes > 4)
{
    ...
}
 
Share this answer
 
v2
Comments
BillWoodruff 11-May-16 9:43am    
+5
George Jonsson 11-May-16 18:33pm    
Thanks Bill.
Sergey Alexandrovich Kryukov 11-May-16 13:02pm    
5ed.
—SA
George Jonsson 11-May-16 18:33pm    
Thanks Sergey.
Try something like
C#
if(DateTime.Now.Subtract(dattme).Minutes > 4)


Subtract gives you an object of Timespan class and u can check the minutes in it
 
Share this answer
 
Comments
George Jonsson 11-May-16 7:03am    
The problem with using the Minutes property is that if the time difference is, for example, 1 hour and 1 minute, the Minutes property will be equal to 1.
So then the if statement will not be entered, even though the file is 61 minutes old.
glen205 11-May-16 7:31am    
Timespan TotalMinutes property should address this, rather than using Minutes?
George Jonsson 11-May-16 7:33am    
super_user 11-May-16 7:36am    
george please explain briefly
George Jonsson 11-May-16 8:56am    
What do you need to be explained?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900