读书人

时间比较:小弟我写了一个定时关机的系

发布时间: 2012-04-05 12:42:40 作者: rapoo

时间比较:我写了一个定时关机的系统,请问如何在timer控件的函数中比较时间,当到时间后关机。
关键是时间的比较!

[解决办法]

探讨
if(DateTime.Now==关机时间)
{关机;}

[解决办法]
前两种方法就不给你写了,只给你写写第三种方法
C# code
//*************************调用API***********************************        [StructLayout(LayoutKind.Sequential, Pack = 1)]            internal struct TokPriv1Luid        {            public int Count;            public long Luid;            public int Attr;        }        [DllImport("kernel32.dll", ExactSpelling = true)]        internal static extern IntPtr GetCurrentProcess();        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);        [DllImport("advapi32.dll", SetLastError = true)]        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,            ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]        internal static extern bool ExitWindowsEx(int flg, int rea);        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;        internal const int TOKEN_QUERY = 0x00000008;        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";        internal const int EWX_LOGOFF = 0x00000000;        internal const int EWX_SHUTDOWN = 0x00000001;        internal const int EWX_REBOOT = 0x00000002;        internal const int EWX_FORCE = 0x00000004;        internal const int EWX_POWEROFF = 0x00000008;        internal const int EWX_FORCEIFHUNG = 0x00000010;        private bool DoExitWin(int flg)        {            bool ok;            TokPriv1Luid tp;            IntPtr hproc = GetCurrentProcess();            IntPtr htok = IntPtr.Zero;            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);            tp.Count = 1;            tp.Luid = 0;            tp.Attr = SE_PRIVILEGE_ENABLED;            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);            ok = ExitWindowsEx(flg, 0);            return ok;        }///        public void logoff()//注销        {             Thread.Sleep((s.Days*24*60*60+s.Hours*60*60+s.Minutes*60)*1000+s.Milliseconds);            DoExitWin(EWX_LOGOFF);        }        public void reboot()//重启        {            Thread.Sleep((s.Days*24*60*60+s.Hours*60*60+s.Minutes*60)*1000+s.Milliseconds);            DoExitWin(EWX_REBOOT);         }        public void Shutdown()//关机        {            Thread.Sleep((s.Days*24*60*60+s.Hours*60*60+s.Minutes*60)*1000+s.Milliseconds);            DoExitWin(EWX_SHUTDOWN);         } 

读书人网 >C#

热点推荐