Jak zmienić datę systemową w Windows 7?

0

Znalazłem taki kod:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ZmienDate
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct SystemTime
        {
            public ushort Year;
            public ushort Month;
            public ushort DayOfWeek;
            public ushort Day;
            public ushort Hour;
            public ushort Minute;
            public ushort Second;
            public ushort Milliseconds;
        }
        [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
        private extern static void Win32GetSystemTime(ref  SystemTime lpSystemTime);

        [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
        [return: MarshalAsAttribute(UnmanagedType.Bool)]
        private extern static bool Win32SetSystemTime(ref  SystemTime lpSystemTime);

        static void Main(string[] args)
        {
            SystemTime updatedTime = new SystemTime();
            Win32GetSystemTime(ref  updatedTime);
            updatedTime.Day = 23;
            updatedTime.Month = 4;
            updatedTime.Year = 2007;
            Win32SetSystemTime(ref  updatedTime);
        }
    }
}

Niestety data w systemie Windows 7 nie zmienia się, dalej jest aktualna. Czy można zmienić datę systemową w jakiś sposób? Czy może data zmieni się dopiero po restarcie komputera czy od razu po wykonaniu programu?

0

I właśnie po tak napisanych programach Vistę i 7 zalała fala negatywów, bo.. programy nie działają. Do zmiany ustawień systemowych, w tym daty, potrzeba uprawnień. Zwykły użytkownik nie ma do tego prawa. Z resztą, jak czytamy na MSDN:

The calling process must have the SE_SYSTEMTIME_NAME privilege. This privilege is disabled by default. The SetSystemTime function enables the SE_SYSTEMTIME_NAME privilege before changing the system time and disables the privilege before returning. For more information, see Running with Special Privileges.

http://msdn.microsoft.com/en-us/library/ms717802%28v=VS.85%29.aspx
Albo wydziel tę funkcję programu do oddzielnej, małej aplikacji i uruchamiaj ją z podwyższonymi prawami za pomocą manifestu. W najprostszym przykładzie, użytkownik będzie musiał za pomocą okna z UAC zaakceptować zmianę i tyle, w najgorszym, po prostu nie zmienisz daty.

1 użytkowników online, w tym zalogowanych: 0, gości: 1