Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

How to set system time in C#?

已查看 21 次
跳至第一个未读帖子

Valer BOCAN

未读,
2001年1月31日 21:50:322001/1/31
收件人
I tried DateTime.Now = .... but it is a read only property. Any ideas?


Eric Gunnerson (MSFT)

未读,
2001年2月2日 14:10:142001/2/2
收件人
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

未读,
2001年2月2日 19:41:042001/2/2
收件人
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)

未读,
2001年2月2日 20:05:342001/2/2
收件人
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

未读,
2001年2月9日 14:04:182001/2/9
收件人
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)

未读,
2001年2月9日 16:03:412001/2/9
收件人
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 个新帖子