Change System Date using C#

In this article, i will teach you how to create a program that will change the system date of your computer. This tutorial is very helpful to your program or system having this integration. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Next, add one DateTimePicker and and one button. You must design your interface like this: output 3. Import first the Runtime.InteropServices to access the DLL of the kernel32 in accessing the system date.
  1. using System.Runtime.InteropServices;
Now, use a structure that will have the variables for the reference of our DLL.
  1.   public struct SystemDate
  2.         {
  3.             public ushort Year;
  4.             public ushort Month;
  5.             public ushort DayOfWeek;
  6.             public ushort Day;
  7.             public ushort Hour;
  8.             public ushort Minute;
  9.             public ushort Second;
  10.             public ushort Millisecond;
  11.         };
  12.  
  13. import the DLL now,
  14. <csharp>
  15.         [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
  16.         public extern static bool Win32SetSystemTime(ref SystemDate sysDate);
  17.         };
then put this code in your button to change directly the system date.
  1.    private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             SystemDate updatedTime = new SystemDate();
  4.             updatedTime.Month = (ushort)dateTimePicker1.Value.Month;
  5.             updatedTime.Day = (ushort)dateTimePicker1.Value.Day;
  6.             updatedTime.Year = (ushort)dateTimePicker1.Value.Year;
  7.            
  8.             //this will update the date in your system date
  9.           Win32SetSystemTime(ref updatedTime);
  10.  
  11.          
  12.          MessageBox.Show("The system date has changed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
  13.         }
Done! Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Add new comment