zmiana daty systemowej C#

0

Witam, chciałbym zmieniać datę systemową pod jakimiś tam warunkami.
na razie dla testów z pomocą wyszukiwarki ma taki kod. Dziwnie mi działa, bo

  1. zawsze zmienia date na 23 kwiecień 2008 pomimo tego co ma w kodzie
  2. kod dokładnie w takiej postaci jak wkleiłem ( czyli sporo pokomentowane ) tak i tak zwraca mi np. msgBoxa. Dlaczego?

godziny nie chcę w ogóle ruszać. Co robię nie tak?

        [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
         public extern static void Win32GetSystemTime(ref SystemTime sysTime);

        [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
         public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            //DateTime dtToday = DateTime.Now;

            // run this code under some condition to set system date and time
            SystemTime updatedTime = new SystemTime();
            updatedTime.Year = (ushort)2011;
            updatedTime.Month = (ushort)4;
            updatedTime.Day = (ushort)23;

            // UTC time; it will be modified according to the regional settings of the target computer so the 
            // actual hour might differ
            //updatedTime.Hour = (ushort)10;
            //updatedTime.Minute = (ushort)0;
            //updatedTime.Second = (ushort)0;
            // Call the unmanaged function that sets the new date and time instantly
            Win32SetSystemTime(ref updatedTime);

            // Retrieve the current system date and time
            //SystemTime currTime = new SystemTime();
            //Win32GetSystemTime(ref currTime);
            // You can now use the struct to retrieve the date and time
            //MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know where your C# code is?");    
            
        }
0

poprawne ustawianie daty:

using System.Runtime.InteropServices;

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 = 2011;
		Win32SetSystemTime(ref updatedTime);
	}
}

pamiętaj, że ustawiając tylko część pól SystemTime możesz zrobić głupotę (resetujesz godzinę, minutę i sekundę i to jeszcze do UTC).

0

dzięki za pomoc!
działa tak jak chciałem, mogę jechać z tym dalej. Jeszcze raz dzięki

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