Exemple #1
0
 private void ChangeDateUp(int numDays)
 {
     DateTime t = DateTime.Now.AddDays(1);
     DateTime h = DateTime.Now.AddHours(0);
     SYSTEMTIME st = new SYSTEMTIME();
     st.FromDateTime(t);
     st.FromDateTime(h);
     SetSystemTime(ref st);
 }
Exemple #2
0
            /// <summary>
            /// 静态方法 转换为SYSTEMTIME
            /// </summary>
            /// <param name="time"></param>
            /// <returns></returns>
            public static SYSTEMTIME NewFromDateTime(DateTime time)
            {
                SYSTEMTIME stime = new SYSTEMTIME();

                stime.FromDateTime(time);
                return(stime);
            }
    // read SQL Time and set time on device
    public static int SyncWithSqlTime(System.Data.SqlClient.SqlConnection con)
    {
        SYSTEMTIME systemTime = new SYSTEMTIME();
        DateTime   sqlTime    = NODATE;
        string     sql        = "SELECT GETDATE() AS [CurrentDateTime]";

        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con)) {
            try {
                cmd.Connection.Open();
                System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader();
                while (r.Read())
                {
                    if (!r.IsDBNull(0))
                    {
                        sqlTime = (DateTime)r[0];
                    }
                }
            } catch (Exception) {
                return(-1);
            }
        }
        if (sqlTime != NODATE)
        {
            systemTime.FromDateTime(sqlTime); // Convert to SYSTEMTIME
            if (SetLocalTime(ref systemTime)) //Call Win32 API to set time
            {
                return(1);
            }
        }
        return(0);
    }
Exemple #4
0
    private void ChangeDateDown(int numDays)
    {
        DateTime   t  = DateTime.Now.AddDays(-1);
        SYSTEMTIME st = new SYSTEMTIME();

        st.FromDateTime(t);
        SetSystemTime(ref st);
    }
 public void SetDiscoveryTime(DateTime dt)
 {
     if (LastSeen != DateTime.MinValue)
     {
         throw new InvalidOperationException("LastSeen is already set.");
     }
     _info.stLastSeen = SYSTEMTIME.FromDateTime(dt);
 }
Exemple #6
0
        /// <summary>
        /// 设置系统时间
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static bool SetSysTime(DateTime datetime)
        {
            //转换System.DateTime到SYSTEMTIME
            SYSTEMTIME st = new SYSTEMTIME();

            st.FromDateTime(datetime);
            //调用Win32 API设置系统时间
            return(Win32API.SetLocalTime(ref st));
        }
        public CTimeChanger(int msOffset)
        {
            DateTime t = DateTime.Now.AddMilliseconds(msOffset);
            //Convert to SYSTEMTIME
            SYSTEMTIME st = new SYSTEMTIME();

            st.FromDateTime(t);
            //Call Win32 API to set time
            SetLocalTime(ref st);
        }
        private void OnSetTimeClicked(object sender, EventArgs e)
        {
            SYSTEMTIME lpLocalTime = new SYSTEMTIME();

            lpLocalTime.FromDateTime(DateTime.Now);
            lpLocalTime.Hour   = Convert.ToInt16(this._tbHour.Text);
            lpLocalTime.Minute = Convert.ToInt16(this._tbMin.Text);
            lpLocalTime.Second = Convert.ToInt16(this._tbSec.Text);
            SetLocalTime(ref lpLocalTime);
            base.Close();
        }
        private static void SetSystemTime(DateTime time)
        {
            var        timeStringArray = time.ToString("HH:mm:ss").Split(':');
            SYSTEMTIME lpLocalTime     = new SYSTEMTIME();

            lpLocalTime.FromDateTime(DateTime.Now);
            lpLocalTime.Hour   = Convert.ToInt16(timeStringArray[0]);
            lpLocalTime.Minute = Convert.ToInt16(timeStringArray[1]);
            lpLocalTime.Second = Convert.ToInt16(timeStringArray[2]);
            SetLocalTime(ref lpLocalTime);
        }
Exemple #10
0
        private bool setTime(DateTime datetime)
        {
            SYSTEMTIME st = new SYSTEMTIME();

            st.FromDateTime(datetime);
            if (SetLocalTime(ref st))
            {
                mTimeSpan = DateTime.Now - DateTime.Now;
                return(true);
            }
            return(false);
        }
 /// <summary>
 /// 设置系统时间
 /// win7需要管理员权限
 /// </summary>
 /// <param name="dateTime"></param>
 /// <param name="exceptionHandler"></param>
 /// <returns></returns>
 public static bool SetSystemTime(DateTime dateTime, Action <Exception> exceptionHandler = null)
 {
     try
     {
         SYSTEMTIME systemtime = SYSTEMTIME.FromDateTime(dateTime);
         return(Win32API.SetLocalTime(ref systemtime));
     }
     catch (Exception e)
     {
         exceptionHandler?.Invoke(e);
         return(false);
     }
 }
Exemple #12
0
        /// <summary>
        /// Edit an existing user notification.
        /// </summary>
        /// <param name="handle">Handle to the notification to overwrite.</param>
        /// <param name="application">String that specifies the name of the application that owns this notification.</param>
        /// <param name="time">The time when the notification should occur.</param>
        /// <param name="notify">Notification object that describes the events that are to occur when the notification time is reached.</param>
        /// <returns>The handle to the notification indicates success.</returns>
        public static int SetUserNotification(int handle, string application, System.DateTime time, UserNotification notify)
        {
            SYSTEMTIME st = SYSTEMTIME.FromDateTime(time);

            //call api function
            int outhandle = NativeMethods.CeSetUserNotification(handle, application, ref st, notify);

            //if invalid handle throw exception
            if (outhandle == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Error setting UserNotification");
            }
            return(outhandle);
        }
Exemple #13
0
        public void SetTimeFunc(double TimeUnix)
        {
            //取得当前系统时间
            DateTime t = ConvertIntDateTime(TimeUnix);

            //对比时间点更新
            SelectPaste_From.ctDateTime = t;

            //转换System.DateTime到SYSTEMTIME
            SYSTEMTIME st = new SYSTEMTIME();

            st.FromDateTime(t);

            //调用Win32 API设置系统时间
            Win32API.SetLocalTime(ref st);
        }
Exemple #14
0
        /// <summary>
        /// This function prompts the system to start running a specified application at a specified time.
        /// </summary>
        /// <param name="appName">Name of the application to be started.</param>
        /// <param name="time">DateTime at which to run application.</param>
        /// <remarks>To cancel an existing RunAppATime request pass the application name and DateTime.MinValue</remarks>
        public static void RunAppAtTime(string appName, System.DateTime time)
        {
            SYSTEMTIME st = new SYSTEMTIME();

            if (time != System.DateTime.MinValue)
            {
                //get native system time struct
                st = new SYSTEMTIME();
                st = SYSTEMTIME.FromDateTime(time);

                if (!NativeMethods.CeRunAppAtTime(appName, ref st))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Cannot Set Notification Handler");
                }
            }
            else
            {
                if (!NativeMethods.CeRunAppAtTimeCancel(appName, null))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Cannot Cancel Notification Handler");
                }
            }
        }