C#中的时差 [英] Time difference in C#

查看:132
本文介绍了C#中的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景中有一个驱动器,并且在该驱动器中有一个文件,该文件每4分钟自动创建一次,因此我可以在此代码中获取此日期时间

现在我想在db中插入数据,当未在4 mint中创建任何文件时,我想在db中插入数据

数据已发送到db,但问题是当4分钟结束并且我想要这种情况时,该数据未发送

我尝试过的事情:

代码

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

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

推荐答案

我认为您已经忘记了相减两次的小细节.
现在,您只需要花一点时间检查一下Minute部分.

试试
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
TimeSpan timeDiff = DateTime.Now - File.GetLastAccessTime(abc);
if (timeDiff.TotalMinutes > 4)
{
    ...
}


尝试类似
的方法
Try something like
if(DateTime.Now.Subtract(dattme).Minutes > 4)



减法为您提供了一个Timespan类的对象,您可以检查其中的分钟数



Subtract gives you an object of Timespan class and u can check the minutes in it


这篇关于C#中的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆