Exemplo n.º 1
0
        private void btnSetDate_Click(object sender, EventArgs e)
        {
            DateTime    setDate  = dtPickerTarget.Value.Date;
            CultureInfo provider = CultureInfo.InvariantCulture;

            try
            {
                DateTime targetTime = DateTime.ParseExact(txtTargetTime.Text, "HH:mm:ss", provider);
                setDate = setDate.AddHours(targetTime.Hour);
                setDate = setDate.AddMinutes(targetTime.Minute);
                setDate = setDate.AddSeconds(targetTime.Second);

                TimeAndDate.SYSTEMTIME sysTime = new TimeAndDate.SYSTEMTIME();
                sysTime.FromDateTime(setDate);
                TimeAndDate.SetLocalTime(ref sysTime);

                setNewSettings();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        private void btnResetDate_Click(object sender, EventArgs e)
        {
            try
            {
                string hostName = "";
                int    cnt      = 1;
                while (hostName != null)
                {
                    try
                    {
                        hostName = ConfigurationManager.AppSettings["server" + cnt];
                        DateTime atomicTime = TimeAndDate.GetAtomicTime(hostName);

                        //UTC offset and daylight saving time?
                        atomicTime = TimeZone.CurrentTimeZone.ToLocalTime(atomicTime);

                        TimeAndDate.SYSTEMTIME sysTime = new TimeAndDate.SYSTEMTIME();
                        sysTime.FromDateTime(atomicTime);
                        TimeAndDate.SetLocalTime(ref sysTime);
                        MessageBox.Show("Date reset!");
                        return;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        cnt++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }