Pobieranie daty z systemu.

0
SystemTime updatedTime = new SystemTime();
                    updatedTime.Year = (ushort)rokUzytkownika;
                    updatedTime.Month = (ushort)DateTime.Now.Month;
                    updatedTime.Day = (ushort)DateTime.Now.Day;
                    updatedTime.Hour = (ushort)DateTime.Now.Hour;
                    updatedTime.Minute = (ushort)DateTime.Now.Minute;
                    updatedTime.Second = (ushort)DateTime.Now.Second;
                    updatedTime.Millisecond = (ushort)DateTime.Now.Millisecond;

Dlaczego to DateTime.Now.Day; nie pobiera aktulnie ustawionego czasu?
updatedTime.Day = (ushort)DateTime.Now.Day;

0

pobiera (i powinno działać), ale ogólnie robisz to źle. DateTime.Now powinieneś pobierać tylko raz, bo tak jak robisz, data ci się aktualizuje cały czas między wywołaniami.
w wyjątkowo pechowym przypadku między pobraniem np. Month a Millisecond może ci przeskoczyć nawet rok.

DateTime now = DateTime.Now;
updatedTime.Month = (ushort)now.Month;
... etc

poza tym w dziwny sposób pobierasz rok. nie wiem co to ma znaczyć, ale na pewno sugeruje błąd projektowy.

0
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ChangeDate
{
    public partial class Form1 : Form
    {
        DateTime now = DateTime.Now;
        public int rokUzytkownika;
        public 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 Millisecond;
        };

        [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);

        public Form1()
        {
            InitializeComponent();
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = null;
            try
            {
                
                SystemTime updatedTime = new SystemTime();
                updatedTime.Year = (ushort)2007;
                updatedTime.Month = (ushort)now.Month;
                updatedTime.Day = (ushort)now.Day;
                updatedTime.Day = (ushort)now.Hour;
                updatedTime.Day = (ushort)now.Minute;
                updatedTime.Day = (ushort)now.Second;
                updatedTime.Day = (ushort)now.Millisecond;
                Win32SetSystemTime(ref updatedTime);
                label1.Text = "Gotowe";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nie udało się zmienić daty. Proszę sprawdzić czy program został włączony z prawami administratora. " + ex, "Komunikat");
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;//odblkowujemy klawisz jeśli jest to 0,1,2,3,4,5,6,7,9
            if (e.KeyChar == '0' ||
                e.KeyChar == '1' ||
                e.KeyChar == '2' ||
                e.KeyChar == '3' ||
                e.KeyChar == '4' ||
                e.KeyChar == '5' ||
                e.KeyChar == '6' ||
                e.KeyChar == '7' ||
                e.KeyChar == '8' ||
                e.KeyChar == '9')
            {
                e.Handled = false;
            }
            if (Convert.ToInt32(e.KeyChar) == 8)//odblokowujemy backspace
            {
                e.Handled = false;
            }
        }

        private void buttonUstaw_Click(object sender, EventArgs e)
        {
            
            label1.Text = null;
            try
            {
                rokUzytkownika = int.Parse(textBox1.Text);
                
                SystemTime updatedTime = new SystemTime();
                updatedTime.Year = (ushort)rokUzytkownika;
                updatedTime.Month = (ushort)now.Month;
                updatedTime.Day = (ushort)now.Day;
                updatedTime.Day = (ushort)now.Hour;
                updatedTime.Day = (ushort)now.Minute;
                updatedTime.Day = (ushort)now.Second;
                updatedTime.Day = (ushort)now.Millisecond;
                Win32SetSystemTime(ref updatedTime);
                label1.Text = "Gotowe";
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nie udało się zmienić daty. Proszę sprawdzić czy program został włączony z prawami administratora. " + ex, "Komunikat");
            }
        }
    }
 
}

Już nie wiem co tu jest źle. Teraz wcale nie zmienia nawet roku, a nie wiem dlaczego, skoro wcześniej rok się zmieniał, tylko był problem, że godzina się zmieniała. Po prostu chce zmieniać sam rok, pozostawiając godzinę i datę.

1

Już nie wiem co tu jest źle.
źle było wiele rzeczy, a konkretnie braki w definicjach struktury i funkcji WinAPI.

using System;
using System.Runtime.InteropServices;

class TimeApplication
{
	[StructLayout(LayoutKind.Sequential)]
	public 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 Millisecond;
	};
 
        [DllImport("kernel32.dll", EntryPoint = "GetSystemTime")]
        public extern static void Win32GetSystemTime([Out] out SystemTime sysTime);
 
        [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public extern static bool Win32SetSystemTime([In] ref SystemTime sysTime);

	static void Main()
	{
		SystemTime st;
		Win32GetSystemTime(out st);
		st.Year = 1999;
		Win32SetSystemTime(ref st);
	}
}

do tego jak już używasz GetSystemTime/SetSystemTime to DateTime do tego nie mieszaj, bo się pogubisz.

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