Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 5 của 5
  1. #1

    Thiết lập thời gian hệ thống trong lập trình C#?

    Chào các bạn, có thể cho mình xin đoạn code có chức năng thay đổi giờ hệ thống sử dụng code C# được không, mình cũng có tham khảo 1 số cách nhưng không được.

    class Tester
    {
    [System.Runtime.InteropServices.DllImport("kernel32 ", SetLastError =
    true)]
    private static extern bool GetSystemTime(out SYSTEMTIME systemTime);
    [System.Runtime.InteropServices.DllImport("kernel32 ", SetLastError =
    true)]
    private static extern bool SetSystemTime(ref SYSTEMTIME systemTime);
    struct SYSTEMTIME {
    internal short wYear;
    internal short wMonth;
    internal short wDayOfWeek;
    internal short wDay;
    internal short wHour;
    internal short wMinute;
    internal short wSecond;
    internal short wMilliseconds;
    }
    static void Main()
    {
    SYSTEMTIME st;
    if(GetSystemTime(out st))
    {
    st.wHour = 13; //Beware SYSTEMTIME is in UTC time format!!!!!
    if(SetSystemTime(ref st))
    Console.WriteLine("success");
    else
    Console.WriteLine(System.Runtime.InteropServices.M arshal.GetLastWin32Error());
    }
    else
    Console.WriteLine("GetSystemTime failed: {0}",
    System.Runtime.InteropServices.Marshal.GetLastWin3 2Error());
    }
    }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi baothanh07
    Bạn gõ tương tự như vầy do mình mới cài lại máy chưa cài VS:
    System.dianotics.process.start("cmd.exe","time 12:00");
    Không đượcc rồi bạn ơi, khi gõ câu lệnh như trên thì chương trình sẽ gọi màn hình Command.

  3. #3
    http://www.geekpedia.com/code117_Get...-And-Time.html

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi qanlh_soft
    http://www.geekpedia.com/code117_Get...-And-Time.html
    cái này thì cũng giống đoạn code của sundragon0220. Đoạn code dưới sẽ lấy chính xác datetime trên PC:
    Mã:
    class LocalTimeManager
        {
            [DllImport("kernel32.dll")]
            private extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);
    
            [DllImport("kernel32.dll")]
            private extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);
    
    
            private struct SYSTEMTIME
            {
                public ushort wYear;
                public ushort wMonth;
                public ushort wDayOfWeek;
                public ushort wDay;
                public ushort wHour;
                public ushort wMinute;
                public ushort wSecond;
                public ushort wMilliseconds;
            }
    
            public void GetTime()
            {
                // Call the native GetSystemTime method
                // with the defined structure.
                SYSTEMTIME stime = new SYSTEMTIME();
                GetLocalTime(ref stime);
    
                // Show the current time.           
                MessageBox.Show("Current Time: " +
                    stime.wHour.ToString() + ":"
                    + stime.wMinute.ToString());
            }
            public void SetTime()
            {
                // Call the native GetSystemTime method
                // with the defined structure.
                SYSTEMTIME systime = new SYSTEMTIME();
                GetLocalTime(ref systime);
    
                // Set the system clock ahead one hour.
                systime.wHour = (ushort)(systime.wHour + 1 % 24);
                SetLocalTime(ref systime);
                MessageBox.Show("New time: " + systime.wHour.ToString() + ":"
                    + systime.wMinute.ToString());
            }
        }
    Sửa lại hàm SetTime theo ý của bạn thôi

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn gõ tương tự như vầy do mình mới cài lại máy chưa cài VS:
    System.dianotics.process.start("cmd.exe","time 12:00");

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •