473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need to chanmge windows system time

1
hello ,

i need to do some things with windows system time .
the problem is that i need to change windows system time - and i dont know
how to change it .

datetime.new - can't get any parameters ..

i would like to get some help ..

thanks
Nov 30 '09 #1
1 7794
tlhintoq
3,525 Expert 2GB
A DateTime data type is not for the System time. It can be any DateTime you might be working with: An appointment might have a DateTime. A birthday. The date and time a file was created. Etc.

The Windows SystemTime is actually a structure you set. There is no .NET Framework feature for this, so you have to do it through a WinAPI call.
A quick Google for "C# Set Windows time" got these two examples:
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential)] 
  2. public struct SYSTEMTIME { 
  3.  public short wYear; 
  4.  public short wMonth; 
  5.  public short wDayOfWeek; 
  6.  public short wDay; 
  7.  public short wHour; 
  8.  public short wMinute; 
  9.  public short wSecond; 
  10.  public short wMilliseconds; 
  11.  } 
  12.  [DllImport("kernel32.dll", SetLastError=true)] 
  13. public static extern bool SetSystemTime(ref SYSTEMTIME theDateTime );
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace Sample
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.  
  17.         public struct SystemTime
  18.         {
  19.             public ushort Year;
  20.             public ushort Month;
  21.             public ushort DayOfWeek;
  22.             public ushort Day;
  23.             public ushort Hour;
  24.             public ushort Minute;
  25.             public ushort Second;
  26.             public ushort Millisecond;
  27.         };
  28.  
  29.         [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
  30.         public extern static void Win32GetSystemTime(ref SystemTime sysTime);
  31.  
  32.         [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
  33.         public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
  34.  
  35.         private void button1_Click(object sender, EventArgs e)
  36.         {
  37.             // Set system date and time
  38.             SystemTime updatedTime = new SystemTime();
  39.             updatedTime.Year = (ushort)2008;
  40.             updatedTime.Month = (ushort)4;
  41.             updatedTime.Day = (ushort)23;
  42.             // UTC time; it will be modified according to the regional settings of the target computer so the actual hour might differ
  43.             updatedTime.Hour = (ushort)10;
  44.             updatedTime.Minute = (ushort)0;
  45.             updatedTime.Second = (ushort)0;
  46.             // Call the unmanaged function that sets the new date and time instantly
  47.             Win32SetSystemTime(ref updatedTime);
  48.  
  49.             // Retrieve the current system date and time
  50.             SystemTime currTime = new SystemTime();
  51.             Win32GetSystemTime(ref currTime);
  52.             // You can now use the struct to retrieve the date and time
  53.             MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know where your C# code is?");
  54.         }
  55.     }
  56. }
Nov 30 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
3
by: Andrej Hristoliubov | last post by:
I am the best c++ programmer in the whole wide world. Trust ME! My reference is Victor Bazarov,Valentin Samko,Alf P.Steinbach( Me and Alf actually intern together at Microsoft), and Bjarne...
5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
1
by: Günther Rühmann | last post by:
Hi, I´m not sure if i´m right int this group... My problem: I made a vb .net application that reads from AD via System.Directoryservices.Directoryentry. The appliocation enumerates group...
9
by: Günther Rühmann | last post by:
Hi, I´m not sure if i´m right int this group... My problem: I made a vb .net application that reads from AD via System.Directoryservices.Directoryentry. The appliocation enumerates group...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
21
by: Tark Siala | last post by:
hi i am using timer control, its good but if my event doing less than one time in 1 Minute. i want make event every 5 minute, how i can do that? i am using VB6, VB.NET -- Best Regards
0
by: Budhi Saputra Prasetya | last post by:
Hi, I still have the same problem with embedding Windows Control. I'll just requote what I posted last time: I managed to create a Windows Form Control and put it on my ASP .NET page. I...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Programs dealing with autoruns Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list programs that help me to view/modify the autoruns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.