C#修改系统时间
using System;
using System.Diagnostics;
namespace shijian
{
internal class Program
{
void SetTime()
{
Process p = new Process();
//Process类有一个StartInfo属性
//设定程序名
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Verb = "runas";
//设定程式执行参数 “/C”表示执行完命令后马上退出
p.StartInfo.Arguments = string.Format("/c time {0}:{1}:{2}", 09, 12, 1.28);
//关闭Shell的使用
p.StartInfo.UseShellExecute = false;
//重定向标准输入
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p.StartInfo.RedirectStandardError = true;
//设置不显示doc窗口
p.StartInfo.CreateNoWindow = true;
//启动
p.Start();
//从输出流取得命令执行结果
p.StandardOutput.ReadToEnd();
}
void SetDate()
{
Process p = new Process();
//Process类有一个StartInfo属性
//设定程序名
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Verb = "runas";
//设定程式执行参数 “/C”表示执行完命令后马上退出
p.StartInfo.Arguments = string.Format("/c date {0}-{1}-{2}", 2209, 10, 1);
//关闭Shell的使用
p.StartInfo.UseShellExecute = false;
//重定向标准输入
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p.StartInfo.RedirectStandardError = true;
//设置不显示doc窗口
p.StartInfo.CreateNoWindow = true;
//启动
p.Start();
//从输出流取得命令执行结果
p.StandardOutput.ReadToEnd();
}
static void Main(string[] args)
{
new Program().SetDate();
new Program().SetTime();
}
}
}