Saturday, June 5, 2010

Get System Time

In the previous post I explained the basics of P/Invoke and how to change the system time by that.
Similarly, the system time can of course also be readout by an API - call:
    public class ZeitLesen
        struct str_Zeit
            public ushort Jahr;
            public ushort Monat;
            public ushort TagInDerWoche;
            public ushort Tag;
            public ushort Stunde;
            public ushort Minute;
            public ushort Sekunde;
            public ushort Millisekunde;

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern void GetSystemTime(out str_Zeit AktuelleZeit);

        public void LeseSystemzeit()
            str_Zeit Zeit = new str_Zeit();
            GetSystemTime(out Zeit);
            DateTime AktuelleZeit = new DateTime(Zeit.Jahr, Zeit.Monat, Zeit.Tag, Zeit.Stunde, Zeit.Minute, Zeit.Sekunde, Zeit.Millisekunde);
If one just wants to read the current date and time though, this can be accomplished with means of .Net much easier by using the class DateTime:
DateTime CurrentDate = DateTime.Now;

No comments:

Post a Comment