How to sync client computer time with a server computer time using C#

How to sync client computer time with a server computer time using C#?
(besides using "use net time \\servername /set /y").
sepknowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveH_UKCommented:
In general this is a very bad thing to do.  Clients using C# will be running on a Windows platform and should have a local, correct time.  This is required for domains to function correctly and the Windows Time Service provides this function.  See http://blogs.bdnet.co.uk/steve/archive/2008/01/06/The-Windows-Time-Service.aspx.

Also, you normally need local administrator rights to change the system time.

Nevertheless, if you have the security privileges you can do this using WMI.

See the following for WMI and scripting references:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov07/hey1116.mspx
http://msdn2.microsoft.com/en-us/library/aa393289(VS.85).aspx

To do this in C#, you need to use the System.Management namespace which exposes WMI functionality.  You will need to use the Invoke method to run the SetDateTime method of the Win32_OperatingSystem object.

See http://msdn.microsoft.com/msdnmag/issues/02/05/WMIMan/ for an intro to WMI in .NET.  and http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.framework.wmi&tid=5388448d-2944-4138-9796-c91350b6510a&p=1 for an example.

You might find that an easier option is to use Platform Invoke with the SetSystemTime Win32 function which is referenced here:

http://msdn2.microsoft.com/en-us/library/ms724942(VS.85).aspx

Here's an old example for Visual Basic (not .NET): http://support.microsoft.com/kb/154009.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sepknowAuthor Commented:
Quite a bit to digest.
Let me go through and come back to you later.
Thanks.
0
SteveH_UKCommented:
You're welcome.  If you get stuck, feel free to ask.  I'll be away from my desk later, but there are other experts too...
0
SteveH_UKCommented:
Thanks for the points :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.