简体   繁体   中英

How do I check is uptime value on datasource with C#

I have csv file like this:

spy,20141118 11:58:41,11:58:41 up 76 days  20:43 ,0.00  0.00  0.00,100%,100%,NO
uk,20141118 11:58:51,11:58:41 up 0 min,0.06 0.02 0.00,99%,98%,YES
...
  1. Hostname, 2. date, 3. uptime, 4. load average, etc...

I need to check uptime value is day or less than 1 day. If uptime is less than 1 day, do something, else(1 day or more) do something. How do I check this?

I can check first value like this(on gridview):

DataRowView drv = e.Row.DataItem as DataRowView;
if (drv["Hostname"].ToString().Equals("spy"))
{
...

But I can't do this for uptime.

Assuming there will only be a mention of days when the uptime is one day or larger:

if (drv["uptime"].ToString().Contains("day"))
{
    // up for one day or more
}
else
{
    // not up for a day yet
}

Not sure how you were getting your csv data so you can ignore or change the first line, but this is how you would convert it from a string to a date value and then compare it to the current time and date.

string data = "spy,20141123 10:58:41,11:58:41 up 76 days  20:43 ,0.00  0.00  0.00,100%,100%,NO";            
DateTime date = DateTime.Parse(data.Split(',')[1].Insert(6, "-").Insert(4, "-"));

if (date.AddDays(1) < DateTime.Now)
{               
    MessageBox.Show("up time more than 1 day");
}
else
{               
     MessageBox.Show("up time less than 1 day");
}      

This compares it to the second. So if the computer has been up for 23 hours 59 minutes and 59 seconds it will still be less than one day.

[EDIT]

I've just realised that you're probably not looking at that date and time value, but rather the day value.

string data = "spy,20141123 12:02:41,11:58:41 up 2 days  20:43 ,0.00  0.00  0.00,100%,100%,NO";            
int day = int.Parse(data.Split(',')[2].Split(' ')[2]);     

if (day > 1)
{               
    MessageBox.Show("up time more than 1 day");
}
else if (day == 1)
{
    MessageBox.Show("up time is 1 day");
}
else
{               
    MessageBox.Show("up time less than 1 day");
}                   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM