473,671 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I set XP System Time?

I want to set the local Date Time of the xp systems in my organization using
C# and a winform application.

From what I have read this is not an easy task anymore. You have to obtain
privlege and then set it from there.

Does anyone have a good C# example on how to do this? I want to sync my
local system to the time of my sql server. I can get the datetime of the
server but I dont know how to set the local time.

I was thinking I could console out and then run DOS set commands but this is
messy and would like to find a nice clean solution.

Nov 16 '05 #1
4 16372
Mike,

Well you would use the time API's in Win32, just make sure your code
has the right permission to execute unmanaged code.

public class Win32
{
private Win32()
{
}

[StructLayout(La youtKind.Sequen tial)]
public struct SystemTime
{
[MarshalAs(Unman agedType.U2)]
public short Year;
[MarshalAs(Unman agedType.U2)]
public short Month;
[MarshalAs(Unman agedType.U2)]
public short DayOfWeek;
[MarshalAs(Unman agedType.U2)]
public short Day;
[MarshalAs(Unman agedType.U2)]
public short Hour;
[MarshalAs(Unman agedType.U2)]
public short Minute;
[MarshalAs(Unman agedType.U2)]
public short Second;
[MarshalAs(Unman agedType.U2)]
public short Milliseconds;
}

[DllImport("kern el32.dll")]
public static extern void GetLocalTime(
out System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern void GetSystemTime(
out System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTi me systemTime);
}

Using these functions you can both set and get the system time. Using the
local time functions, you
get the time with the correct timezone and daylight savingstime. You would
use the GetLocalTime
APi like this:

Win32.SystemTim e sysTime;
if( Win32.GetLocalT ime(out sysTime) )
Console.WriteLi ne("{0}:{1}:{2} ", sysTime.Hour, sysTime.Minute,
sysTime.Second) ;

To set the local time you would create a SystemTime structture, populate it
and pass it using the
ref keywords, instead of the out keyword which GetLocalTime uses, to the
SetLocalTime API.

HTH,

//Andreas
"Mike Kearl" <mk****@hotmail .com> skrev i meddelandet
news:ui******** ********@TK2MSF TNGP09.phx.gbl. ..
I want to set the local Date Time of the xp systems in my organization using C# and a winform application.

From what I have read this is not an easy task anymore. You have to obtain privlege and then set it from there.

Does anyone have a good C# example on how to do this? I want to sync my
local system to the time of my sql server. I can get the datetime of the
server but I dont know how to set the local time.

I was thinking I could console out and then run DOS set commands but this is messy and would like to find a nice clean solution.

Nov 16 '05 #2
Mike,

Please note the error I provided in my previous code. All references
to System.SystemTi me in the API's should be changed to Win32.SystemTim e

//Andreas

"Andreas Håkansson" <andy.h (at) telia.com> skrev i meddelandet
news:uY******** ******@TK2MSFTN GP10.phx.gbl...
Mike,

Well you would use the time API's in Win32, just make sure your code
has the right permission to execute unmanaged code.

public class Win32
{
private Win32()
{
}

[StructLayout(La youtKind.Sequen tial)]
public struct SystemTime
{
[MarshalAs(Unman agedType.U2)]
public short Year;
[MarshalAs(Unman agedType.U2)]
public short Month;
[MarshalAs(Unman agedType.U2)]
public short DayOfWeek;
[MarshalAs(Unman agedType.U2)]
public short Day;
[MarshalAs(Unman agedType.U2)]
public short Hour;
[MarshalAs(Unman agedType.U2)]
public short Minute;
[MarshalAs(Unman agedType.U2)]
public short Second;
[MarshalAs(Unman agedType.U2)]
public short Milliseconds;
}

[DllImport("kern el32.dll")]
public static extern void GetLocalTime(
out System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern void GetSystemTime(
out System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTi me systemTime);
}

Using these functions you can both set and get the system time. Using the
local time functions, you
get the time with the correct timezone and daylight savingstime. You would
use the GetLocalTime
APi like this:

Win32.SystemTim e sysTime;
if( Win32.GetLocalT ime(out sysTime) )
Console.WriteLi ne("{0}:{1}:{2} ", sysTime.Hour, sysTime.Minute,
sysTime.Second) ;

To set the local time you would create a SystemTime structture, populate it and pass it using the
ref keywords, instead of the out keyword which GetLocalTime uses, to the
SetLocalTime API.

HTH,

//Andreas
"Mike Kearl" <mk****@hotmail .com> skrev i meddelandet
news:ui******** ********@TK2MSF TNGP09.phx.gbl. ..
I want to set the local Date Time of the xp systems in my organization using
C# and a winform application.

From what I have read this is not an easy task anymore. You have to

obtain
privlege and then set it from there.

Does anyone have a good C# example on how to do this? I want to sync my
local system to the time of my sql server. I can get the datetime of the server but I dont know how to set the local time.

I was thinking I could console out and then run DOS set commands but

this is
messy and would like to find a nice clean solution.


Nov 16 '05 #3
AHHH.. I was just trying to find the namespace for that and couldn't find
it. that helps a ton :)

I appreciate your help
"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:OD******** ******@TK2MSFTN GP09.phx.gbl...
Mike,

Please note the error I provided in my previous code. All references
to System.SystemTi me in the API's should be changed to Win32.SystemTim e

//Andreas

"Andreas Håkansson" <andy.h (at) telia.com> skrev i meddelandet
news:uY******** ******@TK2MSFTN GP10.phx.gbl...
Mike,

Well you would use the time API's in Win32, just make sure your code
has the right permission to execute unmanaged code.

public class Win32
{
private Win32()
{
}

[StructLayout(La youtKind.Sequen tial)]
public struct SystemTime
{
[MarshalAs(Unman agedType.U2)]
public short Year;
[MarshalAs(Unman agedType.U2)]
public short Month;
[MarshalAs(Unman agedType.U2)]
public short DayOfWeek;
[MarshalAs(Unman agedType.U2)]
public short Day;
[MarshalAs(Unman agedType.U2)]
public short Hour;
[MarshalAs(Unman agedType.U2)]
public short Minute;
[MarshalAs(Unman agedType.U2)]
public short Second;
[MarshalAs(Unman agedType.U2)]
public short Milliseconds;
}

[DllImport("kern el32.dll")]
public static extern void GetLocalTime(
out System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern void GetSystemTime(
out System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTi me systemTime);

[DllImport("kern el32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTi me systemTime);
}

Using these functions you can both set and get the system time. Using the
local time functions, you
get the time with the correct timezone and daylight savingstime. You would use the GetLocalTime
APi like this:

Win32.SystemTim e sysTime;
if( Win32.GetLocalT ime(out sysTime) )
Console.WriteLi ne("{0}:{1}:{2} ", sysTime.Hour, sysTime.Minute,
sysTime.Second) ;

To set the local time you would create a SystemTime structture, populate

it
and pass it using the
ref keywords, instead of the out keyword which GetLocalTime uses, to the
SetLocalTime API.

HTH,

//Andreas
"Mike Kearl" <mk****@hotmail .com> skrev i meddelandet
news:ui******** ********@TK2MSF TNGP09.phx.gbl. ..
I want to set the local Date Time of the xp systems in my organization

using
C# and a winform application.

From what I have read this is not an easy task anymore. You have to

obtain
privlege and then set it from there.

Does anyone have a good C# example on how to do this? I want to sync my local system to the time of my sql server. I can get the datetime of

the server but I dont know how to set the local time.

I was thinking I could console out and then run DOS set commands but

this
is
messy and would like to find a nice clean solution.



Nov 16 '05 #4
ok I got the api's working so that I can read the time but I can't get it to
set. I have admin rights and keep getting an error.. Do you have any code
that would set it so that I can compare?

Mike.
"Mike Kearl" <mk****@hotmail .com> wrote in message
news:ui******** ********@TK2MSF TNGP09.phx.gbl. ..
I want to set the local Date Time of the xp systems in my organization using C# and a winform application.

From what I have read this is not an easy task anymore. You have to obtain privlege and then set it from there.

Does anyone have a good C# example on how to do this? I want to sync my
local system to the time of my sql server. I can get the datetime of the
server but I dont know how to set the local time.

I was thinking I could console out and then run DOS set commands but this is messy and would like to find a nice clean solution.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4641
by: Valiz | last post by:
Hi, I am updating system time with IRIG time source which sends irregular pulses as shown below 11000011111100111111111111111111....(1-IRIG present and 0-IRIG not present) I need to update system time when I find a steady pulse or a series of 1's say for a span of 10-15 seconds. Is there a way to implement this in VB6? Thanks in advance. Valiz.
8
2220
by: Sandy Norton | last post by:
Hi folks, I have been mulling over an idea for a very simple python-based personal document management system. The source of this possible solution is the following typical problem: I accumulate a lot of files (documents, archives, pdfs, images, etc.) on a daily basis and storing them in a hierarchical file system is simple but unsatisfactory:
22
2275
by: Zeng | last post by:
Hi, I'm running ClrProfiler for the first time to profile my web app, and it keeps getting stuck at this msg box: "Waiting for Asp.net to start common language runtime - this is the time to load your test page." even after I launched my app and aspnet_wp.exe is running. Do you know what I need to do to fix it? I also found some old post, a person mentioned that I need to make sure I need to run my aspnet with system account instead. ...
4
2014
by: Mike Kearl | last post by:
I want to set the local Date Time of the xp systems in my organization using C# and a winform application. From what I have read this is not an easy task anymore. You have to obtain privlege and then set it from there. Does anyone have a good C# example on how to do this? I want to sync my local system to the time of my sql server. I can get the datetime of the server but I dont know how to set the local time.
14
3666
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 of date formatting (RFC 822). I have been using a variation of RFC 1123 (just change the time zone to an offset, i.e. "-0800"). It seems to be writing okay, but it's failing to parse. I've tried changing the regional & language settings in my...
2
2351
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If mUIO_Threads(i) Is Nothing Then mUIO_Threads(i) = New System.Threading.Thread(AddressOf mUIO_DAQ(i).InitDAQ) mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr mUIO_Threads(i).IsBackground = False
4
11111
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts the default of True. The Elapsed event handler updates the dialog box with how long before it will close, acting as a timer itself. The dialog has a time to close property which is checked every time the Elapsed event fires. The problem I have...
9
3031
by: Xah Lee | last post by:
REQUIREMENTS FOR A VISUALIZATION SOFTWARE SYSTEM FOR 2010 Xah Lee, 2007-03-16 In this essay, i give a list of requirements that i think is necessary for a software system for creating scientific visualization for the next decade (2007-2017). (for a HTML version with images, please see http://xahlee.org/3d/viz.html )
0
8472
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.