473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 22058
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
2
by: Ben | last post by:
I have several pages written in aspx, but sometime the aspx page return the following error. And it hapeen, the whole web application gives this error, that means all the aspx files get affected....
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
11
by: Mike9900 | last post by:
How can I get the real system date/time if the user fakes the date, for example by setting the system date back. -- Mike
3
by: Carlos Lozano | last post by:
I have requirement to pause the system clock until a certain event (Win XP). Then the clock will be resumed to the real time. I think it can be done using core functions and/or redirecting the...
1
by: shyam vashista | last post by:
i have problem in validation check for system date with server date problem::: If i change my system date as september 30, 2006 and use validation for filling form as current date as oct30,...
5
by: adie | last post by:
hi, can anyone help me out with this. the requirement is to display a time (any time) from a distinct timezone (lets say EST) on a webpage but to make available the conversion to the user local...
15
by: iffa n wanie | last post by:
Develop a library information system that provides the following functionalities: a) Add New Patron b) Search Patron c) Check Out Book The patron ID is unique and is generated automatically...
4
by: anagai | last post by:
I just want to check if a date entered in a textbox is equal to the current system date. I set the date object from the input field like this: dt1=new Date('10/01/2007'); the current system...
2
by: samvb | last post by:
Hi, I have just finished a program that uses ms access and depends on system date to do certain things. just when i was testing it, a friend suggested that i change system date to see how it reacts....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.