PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

How to set System Date in C#

 
 
Indrani
Guest
Posts: n/a
 
      9th Aug 2005
I am working on a C# console application. How can we set the system
date in C#.

Rgds,
Indrani

 
Reply With Quote
 
 
 
 
Visually Seen #
Guest
Posts: n/a
 
      9th Aug 2005
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

 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      9th Aug 2005

"Indrani" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.


 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      9th Aug 2005

"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Indrani" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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.Marshal.GetLastWin32Error());
}
else
Console.WriteLine("GetSystemTime failed: {0}",
System.Runtime.InteropServices.Marshal.GetLastWin32Error());
}
}

Willy.



 
Reply With Quote
 
Indrani
Guest
Posts: n/a
 
      9th Aug 2005
Thank you Willy..

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      9th Aug 2005
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 #" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i have two date fileds Opend date Due date, can i set default on due date so, its always = to open date on my data entry form1 Urgent Mike Saifie Microsoft Access Form Coding 1 9th Mar 2006 02:08 AM
ERROR check if a date set entered violates a previous date set buzz Microsoft Excel Misc 1 21st Jun 2004 09:01 PM
ERROR check if a date set entered violates a previously entered date set buzz Microsoft Excel Misc 1 16th Jun 2004 08:07 PM
ERROR check if a date set entered violates a perviously entered date set buzz Microsoft Excel Misc 0 16th Jun 2004 07:13 PM
ERROR check if a date set entered violates a perviously entered date set buzz Microsoft Excel Misc 0 16th Jun 2004 07:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:54 AM.