Gents, I have the following problem that drives me insane. I've written a service that runs in the background. However, I can't get the DateTime
format right. I must have tried everything to get it right, but to no avail. When the service starts or stops, it will tell me by writing to a log file. The output of the log file looks like this : 1/19/2009 1:48:40 PM Manager: Service Stopped 1/19/2009 1:48:43 PM Manager: Service Started 1/19/2009 2:17:05 PM Manager: Service Stopped 1/19/2009 2:17:29 PM Manager: Service Started 1/19/2009 2:18:02 PM Manager: Service Stopped 1/19/2009 2:20:27 PM Manager: Service Started
Now .. When it writes to the logfile, it uses the above format for DateTime.Now
, while I want to use "dd-mm-yyyy HH:mm:ss" I set the regional setting in Control Panel as well as the Date and Time format. It shows up perfect in my system clock. It still spits out the above to the logfile. Then I tried the following in my code, so set it programmatically :
// The main entry point for the process
static void Main()
{
//Set the culture
CultureInfo newCulture = new CultureInfo("en-US", false);
newCulture.DateTimeFormat.FullDateTimePattern = "dd-mm-yyyy HH:mm:ss";
System.Threading.Thread.CurrentThread.CurrentCulture = newCulture;
System.ServiceProcess.ServiceBase\[\] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase\[\] {new WinService1(), new ySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase\[\] { new RealTime() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
That didn't fix it .. still the same output. When I put a MessageBox
in between somewhere to let me show DateTime.Now.ToString()
, it also comes back with 1/19/2009 2:20:27 PM. Some of the methods in the service report an invalid <code>DateTime</code> conversion when I try the following code :
DateTime time = Convert.ToDateTime("19-01-2009 13:12:56");
It drives me insane. Can please someone point me in the right direction ? Kind regards,