Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set system time in C#?

21 views
Skip to first unread message

Valer BOCAN

unread,
Jan 31, 2001, 9:50:32 PM1/31/01
to
I tried DateTime.Now = .... but it is a read only property. Any ideas?


Eric Gunnerson (MSFT)

unread,
Feb 2, 2001, 2:10:14 PM2/2/01
to
Execute the command using cmd.exe and the Process object.

"Valer BOCAN" <bor...@usa.net> wrote in message
news:#Ni0on$iAHA.1620@tkmsftngp02...

Valer BOCAN

unread,
Feb 2, 2001, 7:41:04 PM2/2/01
to
Not precise enough...
I wrote a C# class that connects to a time server and fetches the time with
a precission in the milliseconds range. If I launch a command interpreter
copy, precision is lost.
I think SetSystemTime can be used, but I am not sure how to access the
function in C#.

"Eric Gunnerson (MSFT)" <eri...@no.spam.microsoft.com> wrote in message
news:uBpiGvUjAHA.1104@tkmsftngp02...

Eric Gunnerson (MSFT)

unread,
Feb 2, 2001, 8:05:34 PM2/2/01
to
Define this struct:

[StructLayout(Layout.Sequential)]
struct SYSTEMTIME {
short year;
short month;
short dayOfWeek;
short day;
short hour;
short minute;
short second;
short milliseconds;
}

Add this to your class:

[DllImport("kernel32.dll")]
static extern bool SetSystemTime(SYSTEMTIME time);

and then create an instance of SYSTEMTIME and call SetSystemTime() with it.

Can you send me your code when you get it done (or perhaps post it to
http://www.gotdotnet.com); it sounds interesting...

"Valer BOCAN" <bor...@usa.net> wrote in message

news:uYTLNoXjAHA.1352@tkmsftngp02...

Valer BOCAN

unread,
Feb 9, 2001, 2:04:18 PM2/9/01
to
Eric, thanks for the tip. Actually, the correct code is below, or at least
that worked for me:

[StructLayoutAttribute(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}

[DllImport("kernel32.dll")]
static unsafe extern bool SetLocalTime(SYSTEMTIME *time);

"Eric Gunnerson (MSFT)" <eri...@no.spam.microsoft.com> wrote in message

news:O10nq1XjAHA.2184@tkmsftngp02...

Eric Gunnerson (MSFT)

unread,
Feb 9, 2001, 4:03:41 PM2/9/01
to
You shouldn't need to use unsafe to declare the function. Try using "ref
SYSTEMTIME" instead, and I think that will work...

"Valer BOCAN" <bor...@usa.net> wrote in message

news:#mei57skAHA.1696@tkmsftngp04...

0 new messages