473,554 Members | 2,335 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 2008
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
4639
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....
8
2217
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...
22
2255
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,...
4
16368
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...
14
3647
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...
3
13537
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
2
2340
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...
4
11097
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...
9
3016
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
7603
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...
0
7528
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...
0
8046
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...
1
7565
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...
0
7891
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5158
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...
0
3564
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...
1
2021
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1138
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.