473,387 Members | 1,693 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,387 software developers and data experts.

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 1033
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(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}

[DllImport("kernel32.dll")]
public static extern void GetLocalTime(
out System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern void GetSystemTime(
out System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTime 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.SystemTime sysTime;
if( Win32.GetLocalTime(out sysTime) )
Console.WriteLine("{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****************@TK2MSFTNGP09.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.SystemTime in the API's should be changed to Win32.SystemTime

//Andreas

"Andreas Håkansson" <andy.h (at) telia.com> skrev i meddelandet
news:uY**************@TK2MSFTNGP10.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(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}

[DllImport("kernel32.dll")]
public static extern void GetLocalTime(
out System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern void GetSystemTime(
out System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTime 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.SystemTime sysTime;
if( Win32.GetLocalTime(out sysTime) )
Console.WriteLine("{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****************@TK2MSFTNGP09.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**************@TK2MSFTNGP09.phx.gbl...
Mike,

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

//Andreas

"Andreas Håkansson" <andy.h (at) telia.com> skrev i meddelandet
news:uY**************@TK2MSFTNGP10.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(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}

[DllImport("kernel32.dll")]
public static extern void GetLocalTime(
out System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern void GetSystemTime(
out System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTime systemTime);

[DllImport("kernel32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTime 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.SystemTime sysTime;
if( Win32.GetLocalTime(out sysTime) )
Console.WriteLine("{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****************@TK2MSFTNGP09.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****************@TK2MSFTNGP09.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
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...
22
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...
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...
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.