How to set System Date in C#

I

Indrani

I am working on a C# console application. How can we set the system
date in C#.

Rgds,
Indrani
 
V

Visually Seen #

Indrani,

I have to say, the console application you want to make is pretty
difficult, since you have to somehow access the user's computer's time
settings.
If you were to distrubute your application for other people to run, I
don't think that they would like it if you changed their computer's
time, so perhaps your 'goal' might be possibly impossible.

However, I figured out how to represent a time; use the DateTime class.
There are 12 constructors for it, so you can be really precise. Here's
an example:

DateTime dt = new DateTime(2005, 8, 9);

Of course, their are quite are number of methods to this class, which
include adding, subtracting, toString, etc.

Hope this helps,

Seen Sharp
 
W

Willy Denoyette [MVP]

Indrani said:
I am working on a C# console application. How can we set the system
date in C#.

Rgds,
Indrani


1. Use PInvoke to call Win32 API SetSystemTime, or
2. use System.Management classes with WMI class Win32_OperatingSystem and
call SetDateTime on that class.

Both require that the caller has been granted SeSystemTimePrivilege and that
this privilege is enabled.
The second is only available on XP and higher.


Willy.
 
W

Willy Denoyette [MVP]

Willy Denoyette said:
1. Use PInvoke to call Win32 API SetSystemTime, or
2. use System.Management classes with WMI class Win32_OperatingSystem and
call SetDateTime on that class.

Both require that the caller has been granted SeSystemTimePrivilege and
that this privilege is enabled.
The second is only available on XP and higher.


Willy.

Here is how using PInvoke...

class Tester
{
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
true)]
private static extern bool GetSystemTime(out SYSTEMTIME systemTime);
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError =
true)]
private static extern bool SetSystemTime(ref SYSTEMTIME systemTime);
struct SYSTEMTIME {
internal short wYear;
internal short wMonth;
internal short wDayOfWeek;
internal short wDay;
internal short wHour;
internal short wMinute;
internal short wSecond;
internal short wMilliseconds;
}
static void Main()
{
SYSTEMTIME st;
if(GetSystemTime(out st))
{
st.wHour = 13; //Beware SYSTEMTIME is in UTC time format!!!!!
if(SetSystemTime(ref st))
Console.WriteLine("success");
else
Console.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
else
Console.WriteLine("GetSystemTime failed: {0}",
System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
}

Willy.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

And this solved the OP how? :)

Beside Willy post ( which does indeed solve the problem ) there is a site
http://www.pinvoke.net where you can find a huge collection of signatures to
p/invoke


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top