我想返回一個字符串,這是語法正確的,以顯示當前的正常運行時間。例如,3年,7個月,11天,1小時,16分鐘和零秒,這意味着單數單位不應該是複數,零單位應該是複數,但是如果它還沒有出現則不應該顯示零(例如,不顯示年,月等,如果它還沒有發生)在語法上糾正正常運行時間在C#
由於ticks方法將不會工作超過一個月,我使用ManagementObject,但我很困惑如何做的日期時間計算和分解(我對C#很新,所以我正在做一個實用程序,做各種功能,所以我可以學習各種各樣的東西。
這是我現在所擁有的,它不是' t heck很多...
任何幫助,非常感謝。
public string getUptime()
{
// this should be grammatically correct, meaning, 0 and greater than 1 should be plural, one should be singular
// if it can count in realtime, all the better
// in rare case, clipping can occur if the uptime is really, really huge
// you need a function that stores the boot time in a global variable so it's executed once so repainting this won't slow things down with quer
SelectQuery query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary='true'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get())
{
// this is the start time, do math to figure it out now, hoss
DateTime boot = ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
// or is it better to break up everything into days/hours/etc, i think its better to do that at the end before it gets returned
}
string now = DateTime.Now.ToShortDateString();
return "3 years, 7 months, 11 days, 1 hour, 16 minutes and zero seconds"; // long string for testing label width
}
只是可以肯定,你從來沒有打算以其他語言做這項工作嗎? – 2011-03-02 16:33:21