简体   繁体   English

我如何使用C#检查数据源的正常运行时间值

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

I have csv file like this: 我有这样的csv文件:

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... 主机名,2。日期,3。正常运行时间,4。平均负载等...

I need to check uptime value is day or less than 1 day. 我需要检查正常运行时间是一天还是少于1天。 If uptime is less than 1 day, do something, else(1 day or more) do something. 如果正常运行时间少于1天,则执行某项操作,否则(1天或更长)执行某项操作。 How do I check this? 我该如何检查?

I can check first value like this(on gridview): 我可以这样检查第一个值(在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. 不确定如何获取csv数据,以便可以忽略或更改第一行,但这是将字符串从字符串转换为日期值,然后将其与当前时间和日期进行比较的方式。

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. 因此,如果计算机已启动23小时59分59秒,则仍然不到一天。

[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");
}                   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#如何检查数据源是否为空? - c# how do i check if datasource is null? 如何检查c#中是否将值分配给int? - How do I check if a value is assigned to an int in c#? 如何检查C#中未选择ListBox的选定值? - How do I check if selected value of the ListBox is not selected in C#? 如何将ComboBox文本设置为不在DataSource(C#)中的值? - How can I set ComboBox text to a value not in DataSource (C#)? 使用C#插入值时,如何检查数据库中是否已存在值 - How do I check if value already exists in the database when I insert values using C# 如何使用 c# 编写 selenium 脚本并使其自动化以查找网站的停机时间和正常运行时间? - How can I write a selenium script with c# and automate it to find the downtime and uptime of a website? C# - 如何检查字节值是否与指定标志枚举中的任何标志匹配? - C# - how do I check if a byte value matches against any flags in a specified flags enum? 如何从控制台对照数组元素检查用户输入值? C# - How do I check a users input value from the console against an array element? c# 如何检查C#中COM对象的属性X是否具有值Y? - How do I check if Property X of a COM object has value Y in C#? C#:如何在向导下一步之前检查字段的值? (NextButtonClick) - C#: How do I check the value of a field before Wizard's next step? (NextButtonClick)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM