tags:

views:

2283

answers:

4

How do I set the date/time of the computer in C#?

A: 

You will need to call the SetSystemTime Win32 API function through P/Invoke.

Greg Hewgill
+3  A: 

You'll have to use a P/Invoke to the Windows API.

Here's some example code in C#

Luke
A: 

Here's a bit of code to show how to use the Pinvoke.

Jon Dewees
+3  A: 

You can set the date using Microsoft.VisualBasic.Today and Microsoft.VisualBasic.TimeOfDay, although they're subject to security restrictions.

Yes, it's strange accessing the Microsoft,VisualBasic namespace, but there doesn't appear to be a C# equivalent.

R. Bemrose
It's just a .NET framework namespace. C# is not going to get infected by VB-ism just by using classes from Microsoft.VisualBasic. The J# libraries have several goodies too.
Hans Passant