在C#中修改系统时间,,

在C#中除了调用api和cmd之外还有其它的方法修改系统时间吗

在windows平台上,所有的方法归根结底都是调用api。所谓其它方法,就是把这个调用的过程封装起来,让你不直接去写罢了。

  利用WINAPI的public static extern bool SetLocalTime( ref SYSTEMTIME Time)实现public struct SYSTEMTIME{    public ushort wYear;    public ushort wMonth;    public ushort wDayOfWeek;    public......
答案就在这里: C#修改系统时间
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

1
using System;
002
using System.Drawing;
003
using System.Collections;
004
using System.ComponentModel;
005
using System.Windows.Forms;
006
using System.Data;
007
using System.Runtime.InteropServices;
008
namespace 获取和设置系统时间
009
{
010
///
011
/// Form1 的摘要说明。
012
///
013
public class Form1 : System.Windows.Forms.Form
014
{
015
private System.Windows.Forms.GroupBox groupBox1;
016
private System.Windows.Forms.TextBox textBox1;
017
private System.Windows.Forms.GroupBox groupBox2;
018
private System.Windows.Forms.Button button1;
019
private System.Windows.Forms.Button button2;
020
private System.Timers.Timer timer1;
021
private System.Windows.Forms.DateTimePicker setDate;
022
private System.Windows.Forms.DateTimePicker setTime;
023
private System.ComponentModel.IContainer components;
024
[StructLayout(LayoutKind.Sequential)]
025
public struct SystemTime
026
{
027
public ushort wYear;
028
public ushort wMonth;
029
public ushort wDayOfWeek;
030
public ushort wDay;
031
public ushort wHour;
032
public ushort wMinute;
033
public ushort wSecond;
034
public ushort wMiliseconds;
035
}
036

037
// 用于设置系统时间
038
[DllImport("Kernel32.dll")]
039
public static extern bool SetLocalTime( ref SystemTime sysTime );
040
// 用于获得系统时间
041
[DllImport("Kernel32.dll")]
042
public static extern void GetLocalTime(ref SystemTime sysTime);
043
public Form1()
044
{
045
//
046
// Windows 窗体设计器支持所必需的
047
//
048
InitializeComponent();
049

050
//
051
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
052
//
053
}
054

055
///
056
/// 清理所有正在使用的资源。
057
///
058
protected override void Dispose( bool disposing )
059
{
060
if( disposing )
061
{
062
if (components != null)
063
{
064
components.Dispose();
065
}
066
}
067
base.Dispose( disposing );
068
}
069

070
#region Windows 窗体设计器生成的代码
071
///
072
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
073
/// 此方法的内容。
074
///
075
private void InitializeComponent()
076
{
077
this.groupBox1 = new System.Windows.Forms.GroupBox();
078
this.textBox1 = new System.Windows.Forms.TextBox();
079
this.groupBox2 = new System.Windows.Forms.GroupBox();
080
this.setTime = new System.Windows.Forms.DateTimePicker();
081
this.setDate = new System.Windows.Forms.DateTimePicker();
082
this.button1 = new System.Windows.Forms.Button();
083
this.button2 = new System.Windows.Forms.Button();
084
this.timer1 = new System.Timers.Timer();
085
this.groupBox1.SuspendLayout();
086
this.groupBox2.SuspendLayout();
087
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
088
this.SuspendLayout();
089
//
090
// groupBox1
091
//
092
this.groupBox1.Controls.Add(this.textBox1);
093
this.groupBox1.Location = new System.Drawing.Point(32, 24);
094
this.groupBox1.Name = "groupBox1";
095
this.groupBox1.Size = new System.Drawing.Size(216, 64);
096
this.groupBox1.TabIndex = 0;
097
this.groupBox1.TabStop = false;
098
this.groupBox1.Text = "系统当前时间";
099
//
100
// textBox1
101
//
102
this.textBox1.Location = new System.Drawing.Point(16, 24);
103
this.textBox1.Name = "textBox1";
104
this.textBox1.ReadOnly = true;
105
this.textBox1.Size = new System.Drawing.Size(184, 21);
106
this.textBox1.TabIndex = 1;
107
this.textBox1.Text = "";
108
//
109
// groupBox2
110
//
111
this.groupBox2.Controls.Add(this.setTime);
112
this.groupBox2.Controls.Add(this.setDate);
113
this.groupBox2.Location = new System.Drawing.Point(32, 112);
114
this.groupBox2.Name = "groupBox2";
115
this.groupBox2.Size = new System.Drawing.Size(216, 64);
116
this.groupBox2.TabIndex = 1;
117
this.groupBox2.TabStop = false;
118
this.groupBox2.Text = "时间设置为";
119
//
120
// setTime
121
//
122
this.setTime.Format = System.Windows.Forms.DateTimePickerFormat.Time;
123
this.setTime.Location = new System.Drawing.Point(128, 24);
124
this.setTime.Name = "setTime";
125
this.setTime.ShowUpDown = true;
126
this.setTime.Size = new System.Drawing.Size(72, 21);
127
this.setTime.TabIndex = 1;
128
this.setTime.TabStop = false;
129
//
130
// setDate
131
//
132
this.setDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
133
this.setDate.Location = new System.Drawing.Point(8, 24);
134
this.setDate.Name = "setDate";
135
this.setDate.Size = new System.Drawing.Size(104, 21);
136
this.setDate.TabIndex = 0;
137
//
138
// button1
139
//
140
this.button1.Location = new System.Drawing.Point(40, 200);
141
this.button1.Name = "button1";
142
this.button1.Size = new System.Drawing.Size(64, 32);
143
this.button1.TabIndex = 2;
144
this.button1.Text = "设置";
145
this.button1.Click += new System.EventHandler(this.button1_Click);
146
//
147
// button2
148
//
149
this.button2.Location = new System.Drawing.Point(168, 200);
150
this.button2.Name = "button2";
151
this.button2.Size = new System.Drawing.Size(64, 32);
152
this.button2.TabIndex = 3;
153
this.button2.Text = "退出";
154
this.button2.Click += new System.EventHandler(this.button2_Click);
155
//
156
// timer1
157
//
158
this.timer1.Enabled = true;
159
this.timer1.SynchronizingObject = this;
160
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
161
//
162
// Form1
163
//
164
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
165
this.ClientSize = new System.Drawing.Size(280, 261);
166
this.Controls.Add(this.button2);
167
this.Controls.Add(this.button1);
168
this.Controls.Add(this.groupBox2);
169
this.Controls.Add(this.groupBox1);
170
this.Name = "Form1";
171
this.Text = "获取和设置系统时间";
172
this.groupBox1.ResumeLayout(false);
173
this.groupBox2.ResumeLayout(false);
174
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
175
this.ResumeLayout(false);
176

177
}
178
#endregion
179
///
180
/// 应用程序的主入口点。
181
///
182
[STAThread]
183
static void Main()
184
{
185
Application.Run(new Form1());
186
}
187

188
private void button2_Click(object sender, System.EventArgs e)
189
{
190
this.Close(); // 关闭当前窗体
191
}
192

193
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
194
{
195
// 清除textBox1上的字符串
196
textBox1.Clear();
197
// 创建SystemTime结构体,用于接收系统当前时间
198
SystemTime systemTime = new SystemTime();
199
GetLocalTime(ref systemTime); // 获得系统的时间并存在SystemTime结构体中
200
// 将系统的时间从 SystemTime 结构体中中取出,并显示在textBox1上
201
textBox1.Text += systemTime.wYear.ToString() +"-";
202
textBox1.Text += systemTime.wMonth.ToString() + "-";
203
textBox1.Text += systemTime.wDay.ToString() + " ";
204
textBox1.Text += systemTime.wHour.ToString() + ":";
205
textBox1.Text += systemTime.wMinute.ToString() + ":";
206
textBox1.Text += systemTime.wSecond.ToString();
207
// textBox1.Refresh();

208
}
209
private void button1_Click(object sender, System.EventArgs e)
210
{
211
// 创建SystemTime结构体,用于接收用户设置的时间
212
SystemTime systemTime = new SystemTime();
213
// 从setDate,setTime控件中获取年,月,日,小时,分钟,秒信息,存入SystemTime结构体中
214
systemTime.wYear = (ushort)setDate.Value.Year;
215
systemTime.wMonth = (ushort)setDate.Value.Month;
216
systemTime.wDay = (ushort)setDate.Value.Day;
217
systemTime.wHour = (ushort)setTime.Value.Hour;
218
systemTime.wMinute = (ushort)setTime.Value.Minute;
219
systemTime.wSecond = (ushort)setTime.Value.Second;
220
// 将系统的时间设置为用户指定的时间
221
SetLocalTime(ref systemTime);
222
}
223
}
224
}