How to Set systemtime in Windows Mobile Using C #

Source: Internet
Author: User
Introduction

In this snippet, I will explain how to set system time Pocket PC emulator or device using real world time server to using C #. sometimes you may be need to set real time to device, on that time you have to connect to external website or timer providers using GPRS and get the time from the server and then set to device.

Implementation

There are two part of this implementation, first I will create class for read the time from live server and set time in Pocket PC.

Using system; using system. collections. generic; using system. text; using system. runtime. interopservices; using system. net; using system. io; using system. XML; using system. globalization; namespace timemanager {internal class timeutils {# region 'P/invoke method signtures. '[dllimport ("coredll. DLL ", setlasterror = true)] private extern static uint setsystemtime (ref systemtime lpsystemtime); public struct systemtime {public ushort wyear; Public ushort wmonth; Public ushort wdayofweek; Public ushort wday; public ushort whour; Public ushort wminute; Public ushort wsecond; Public ushort wmilliseconds;} # endregion # region 'public Methods '# region // set system local time from the server time. public static bool setlocaltime (string requesturl) {try {double dtimesecond = 0.0; // convert timestamp to time datetime odatetime = new datetime (1970, 1, 1, 0, 0, 0, 0, 0); timesyncservice. timesyncservice otimesyncservice = new NetworkManager. timesyncservice. timesyncservice (); requesturl = string. concat (requesturl, "timesyncservice. asmx "); otimesyncservice. url = requesturl; dtimesecond = otimesyncservice. getcurrenttime (); If (dtimesecond = 0.0) {return false;} // get current timezone ocurrenttimezone = timezone. currenttimezone; // dtimesecond = dtimesecond-(4.5*60*60); odatetime = odatetime. addseconds (dtimesecond); // check isdaylighttime if (ocurrenttimezone. isdaylightsavingtime (odatetime) {daylighttime = ocurrenttimezone. getdaylightchanges (odatetime. year); // here we want to put more work .} timespan otimeoffset = ocurrenttimezone. getutcoffset (odatetime); odatetime = odatetime. addhours (otimeoffset. totalhours); // assign server date and tiemvalues to system file time systemtime when IME = new systemtime (); When ime. wyear = (ushort) odatetime. year; required ime. wmonth = (ushort) odatetime. month; required ime. wday = (ushort) odatetime. day; When ime. whour = (ushort) odatetime. hour; your ime. wminute = (ushort) odatetime. minute; policime. wsecond = (ushort) odatetime. second; // if not given error, we will get set is true; setsystemtime (ref required IME); Return true;} catch (exception ex) {return false ;}} # endregion public static bool setlocaltimefromyahoo (string requesturl) {bool iscorrectdate = false; try {double currenttimestamp = 0; webrequest Req = webrequest. create (requesturl); req. proxy = WebProxy. getdefaproxy proxy (); webresponse res = req. getresponse (); // save as timestamp as a temporary XML file string tempfile = guid. newguid (). tostring () + ". XML "; streamwriter Sw = new streamwriter (tempfile); Sw. write (New streamreader (res. getresponsestream ()). readtoend (); Sw. close (); // read the XML file and get the timestamp value xmltextreader myxml = new xmltextreader (tempfile); While (myxml. read () {Switch (myxml. nodetype) {Case xmlnodetype. element: If (myxml. name = "timestamp") {currenttimestamp = convert. todouble (myxml. readinnerxml ();} break;} myxml. close (); // Delete the temporary XML file fileinfo tfile = new fileinfo (tempfile); tfile. delete (); // convert timestamp to time datetime mydatetime = new datetime (1970, 1, 1, 0, 0, 0, 0); mydatetime = mydatetime. addseconds (currenttimestamp); int mydate = mydatetime. month; int myyear = mydatetime. year; // August or greate than and year if (mydate> = 8 & myyear> = 2007) {iscorrectdate = false ;} else {// Change the system time systemtime when IME = new systemtime (); When ime. wyear = (ushort) mydatetime. year; required ime. wmonth = (ushort) mydatetime. month; required ime. wday = (ushort) mydatetime. day; When ime. whour = (ushort) mydatetime. hour; your ime. wminute = (ushort) mydatetime. minute; policime. wsecond = (ushort) mydatetime. second; setsystemtime (ref systime); iscorrectdate = true;} return iscorrectdate;} catch (exception ex) {iscorrectdate = false; return iscorrectdate; }}# endregion }}

Lets call this utility in your project. The sample like following,

 
Private void settime () {string swebyahootimeserverurl = "http://developer.yahooapis.com/TimeService/V1/getTime? Appid = yahoodemo "; bool bissystemtime = timeutils. setlocaltimefromyahoo (swebyahootimeserverurl); If (bissystemtime) {MessageBox. Show (" Time has been set ");}}

I hope this is help to you. If you have any questions please post in codegain message boards.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.