By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,930 Members | 1,424 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,930 IT Pros & Developers. It's quick & easy.

How can I change the System Time?

P: n/a
I want to change my System clock with an c# programm, but
dateTime are readonly, or?

thanx for help
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
revna, you will have to use p/invoke to do this. Here's the code you'll
need. First the struct:

[StructLayout(Layout.Sequential)]
struct SYSTEMTIME {
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}

And second, here's the function you need to use.

[DllImport("kernel32.dll")]
static extern bool SetSystemTime(ref SYSTEMTIME time);

--
Greg Ewing [MVP]
http://www.citidc.com
"revna" <re***@gmx.de> wrote in message
news:01****************************@phx.gbl...
I want to change my System clock with an c# programm, but
dateTime are readonly, or?

thanx for help

Nov 15 '05 #2

P: n/a
Hi,
The .NET framework doesn't have managed interfaces for all of Win32 API, so
you have to write special code to use things that are built into Windows.
Platform Invoke (P/Invoke) is the most common way to do this. To use
P/Invoke, you write a prototype that describes how the function should be
called, and then the runtime uses this information to make the call. The
other way to do this is by wrapping the functions using the Managed
Extensions to C++.

Here is what you can do to change the system time: (Look at this article
"Using Win32 and Other Libraries" by Eric Gunnerson on MSDN for more info.)

using System;
using System.Runtime.InteropServices;

public struct SystemTime {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
public class MyTimeAdjuster
{
[DllImport("kernel32.dll")]
public static extern bool GetSystemTime(ref SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref SystemTime systemTime);

void SetNewTime(short hour, short minutes)
{
SystemTime st = new SystemTime();
GetSystemTime(ref st);
st.wHour = hour;
st.wMinute = minutes;
SetSystemTime(ref st);
}

public static void Main(string[] args)
{
MyTimeAdjuster myTime = new MyTimeAdjuster();
myTime.SetNewTime( 12, 50);
}
}

Hope this helps,
Martha

"revna" <re***@gmx.de> wrote in message
news:01****************************@phx.gbl...
I want to change my System clock with an c# programm, but
dateTime are readonly, or?

thanx for help

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.