I’ve written a snippet of code to compute the uptime of my win2k machine (i don’t have uptime like as in winxp).
[code lang="java"] // author: Lawrence Oluyede // date: 18 Feb 2004 // license: throw away code
using System; using System.Text;
public class Uptime { public static int Main(string[] argv) { if(argv.Length == 0) { Console.WriteLine(getUptime()); return 0; }
else if(argv.Length == 1 && argv[0].ToLower() == "-v")
{
displayVersion();
return 0;
}
Console.WriteLine("usage: uptime [-v]\n -v display version");
return 1;
}
private static string getUptime() { StringBuilder buffer = new StringBuilder();
int ticks = Environment.TickCount;
// append the hour:minute:second
buffer.Append(DateTime.Now.ToString("HH:MM:ss",
System.Globalization.DateTimeFormatInfo.InvariantInfo));
buffer.Append(" up");
// compute and append the number of days
int days = ticks / (1000 * 60 * 60 * 24);
if(days > 0)
{
buffer.AppendFormat(" {0} day{1}",
days,
days > 1 ? "s" : String.Empty);
}
// compute seconds, minutes and hours
int seconds = ticks / 1000;
int minutes = ticks / (1000 * 60);
int hours = minutes / 60;
hours = hours % 24;
minutes = minutes % 60;
seconds = seconds % 60;
buffer.AppendFormat(" {0,2:D2}:{1,2:D2}:{2,2:D2}", hours, minutes, seconds);
return buffer.ToString();
}
private static void displayVersion() { Console.WriteLine(”uptime v0.1″); Console.WriteLine(” written by Lawrence Oluyede”); } } [/code]

