472,119 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

How to set System Date in C#

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

Rgds,
Indrani

Nov 17 '05 #1
5 21819
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

Nov 17 '05 #2

"Indrani" <no*********@rediffmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.
Nov 17 '05 #3

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...

"Indrani" <no*********@rediffmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.


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.M arshal.GetLastWin32Error());
}
else
Console.WriteLine("GetSystemTime failed: {0}",
System.Runtime.InteropServices.Marshal.GetLastWin3 2Error());
}
}

Willy.

Nov 17 '05 #4
Thank you Willy..

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Nov 17 '05 #5
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Visually Seen #" <al*****************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by NicK chlam via DotNetMonster.com | last post: by
11 posts views Thread by Mike9900 | last post: by
3 posts views Thread by Carlos Lozano | last post: by
15 posts views Thread by iffa n wanie | last post: by
4 posts views Thread by anagai | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.