当前位置:编程学习 > C#/ASP.NET >>

在Win7系统下使用SetLocalTime函数设置系统时间总是失败

大家好,我现在需要修改系统的时间和日期,调用Win32的API SetLocalTime函数,但是这个函数总是返回false,修改不成功,我在网上看到说执行这个函数之前需要调用者必须具有SE_SYSTEMTIME_NAME权限,函数的执行才会成功. 我的系统是win7系统,请问这个如何设置呢?

我的代码如下:
    [StructLayout(LayoutKind.Sequential)]
    public class SystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMiliseconds;
    }

    public class McsfWin32
    {
        [DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "SetLocalTime")]
        public static extern bool SetSystemTime(ref SystemTime lpSystemTime);

        [DllImport("Kernel32.dll ")]
        public static extern void GetSystemTime(ref SystemTime sysTime);

        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);

        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime sysTime);
    }

        public int SetSystemDateAndTime(string SelectedDate, string SelectedTime, out string ResultDescription)
        {
            try
            {
                SystemTime st = new SystemTime();

                string SelectedYear = SelectedDate.Substring(0, SelectedDate.IndexOf(@"/"));
                string SelectedMonth = SelectedDate.Substring(SelectedDate.IndexOf(@"/") + 1, SelectedDate.LastIndexOf(@"/") - SelectedDate.IndexOf(@"/") - 1);
                string SelectedDay = SelectedDate.Substring(SelectedDate.LastIndexOf(@"/") + 1, SelectedDate.Length - SelectedDate.LastIndexOf(@"/") - 1);

                string SelectedHour = SelectedTime.Substring(0, SelectedTime.IndexOf(@":"));
                string SelectedMinute = SelectedTime.Substring(SelectedTime.IndexOf(@":") + 1, SelectedTime.LastIndexOf(@":") - SelectedTime.IndexOf(@":") - 1);
                string SelectedSecond = SelectedTime.Substring(SelectedTime.LastIndexOf(@":") + 1, SelectedTime.Length - SelectedTime.LastIndexOf(@":") - 1);

                st.wYear = Convert.ToUInt16(SelectedYear.Trim());
                st.wMonth = Convert.ToUInt16(SelectedMonth.Trim());
                st.wDay = Convert.ToUInt16(SelectedDay.Trim());
                st.wHour = Convert.ToUInt16(SelectedHour.Trim());
                st.wMinute = Convert.ToUInt16(SelectedMinute.Trim());
                st.wSecond = Convert.ToUInt16(SelectedSecond.Trim());

                bool result = McsfWin32.SetLocalTime(ref st);
                if (result)
                    ResultDescription = "Set system date and time successful!";
                else
                    ResultDescription = "Set system date and time failed!";

                return 0;
            }
            catch (Exception Ex)
            {
                ResultDescription = Ex.Message.ToString();

                return 1;
            }
        }


请各位大虾指点,高分送上哦! --------------------编程问答-------------------- 程序以管理员的身份运行了吗 --------------------编程问答-------------------- 修改时间需要使用管理员登陆,和你的程序无关 --------------------编程问答-------------------- 以管理员权限运行可以修改时间,问题是我现在想把这个函数做成DLL,所以希望能在代码中设置管理员权限,请问通过代码来设置管理员权限,因为我的这段代码很多程序需要去调用,所以请高手指点一下。 --------------------编程问答-------------------- http://blog.csdn.net/sunny243788557/article/details/4947530
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,