如何让程序每次启动的时候自动更新系统时间

ShiningG 2006-07-12 02:42:42
搜索了半天也没找到啊,哪位老兄帮帮忙哦。
...全文
1221 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
coowoo 2006-07-13
  • 打赏
  • 举报
回复
p.StartInfo.FileName = "cmd.exe";
不需要先执行cmd的
直接执行
net
w32tm
就可以了
ShiningG 2006-07-12
  • 打赏
  • 举报
回复
public string ExeCommand()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine("net time /setsntp:time.windows.com");
p.StandardInput.WriteLine("w32tm /once");
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
strOutput = e.Message;
}
return strOutput;

}

帮忙看看我写的有问题吗?使用后没起作用。
coowoo 2006-07-12
  • 打赏
  • 举报
回复
用命令:
net time
w32tm

参见
http://www.microsoft.com/technet/prodtechnol/windowsserver2003/zh-chs/library/ServerHelp/de98cbdd-7d89-4896-9446-0df8f731b038.mspx?mfr=true

http://www.microsoft.com/technet/prodtechnol/windowsserver2003/zh-chs/library/ServerHelp/de98cbdd-7d89-4896-9446-0df8f731b038.mspx?mfr=true
具体例子参见
http://www.ecampus.fudan.edu.cn/ecampus/major/ntp.jsp
ShiningG 2006-07-12
  • 打赏
  • 举报
回复
我的意思是通过INTERNET来更新系统时间,就像WINDOWS更新时间一个道理。
zhuwenbing 2006-07-12
  • 打赏
  • 举报
回复
读取、设置系统时间日期
有多种方法可以读取设置系统时间日期,现介绍两种简单的方法,第一种方法只能读取不能设置。

一、通过调用System.DateTime.Now来得到系统当前时间日期

MessageBox.Show(System.DateTime.Now.Year.ToString() +"-" + System.DateTime.Now.Month.ToString("#00") +"-" +

System.DateTime.Now.Day.ToString("#00") +" " + System.DateTime.Now.DayOfWeek.ToString() +" "

+System.DateTime.Now.Hour.ToString("#00") +":" + System.DateTime.Now.Minute.ToString("#00") +":" +

System.DateTime.Now.Second.ToString("#00"));

二、通过自定义类来读取、设置系统时间日期

//引入名字空间
using System.Runtime.InteropServices
//申明
[DllImport("kernel32.dll", EntryPoint="GetLocalTime")]
public static extern void GetLocalTime (ref SystemTimeDate lpSystemTime);
[DllImport("kernel32.dll", EntryPoint="SetLocalTime")]
public static extern int SetLocalTime (ref SystemTimeDate lpSystemTime );
//定义时间日期类
public struct SystemTimeDate
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
//读取系统时间日期
private void GetTime_Click(object sender, System.EventArgs e)
{
//将当前时间日期显示在窗体标题上
SystemTimeDate st=new SystemTimeDate();
GetLocalTime(ref st);
this .Text="The Date and DateTime is: " ;
this .Text=this.Text+st.wYear.ToString()+"年";
this .Text=this.Text+st.wMonth.ToString()+"月";
this .Text=this.Text+st.wDay.ToString()+"日 ";
this .Text=this.Text+"星期"+st.wDayOfWeek.ToString();
this .Text=this.Text+" "+st.wHour.ToString()+":";
this .Text=this.Text+st.wHour.ToString()+":";
this .Text=this.Text+st.wMinute.ToString()+":";
this .Text=this.Text+st.wSecond.ToString()+".";
this .Text=this.Text+st.wMilliseconds.ToString();
}
//设置系统时间日期
private void GetTime_Click(object sender, System.EventArgs e)
{
//将DateTimePicker控件的值通过自定义的时间日期类来保存到系统时间
SystemTimeDate st=new SystemTimeDate();
st.wYear=(ushort)this.dateTimePicker1.Value.Year;
st.wMonth=(ushort)this.dateTimePicker1.Value.Month;
st.wDay=(ushort)this.dateTimePicker1.Value.Day;
st.wHour=(ushort)this.dateTimePicker1.Value.Hour;
st.wMinute=(ushort)this.dateTimePicker1.Value.Minute;
st.wSecond=(ushort)this.dateTimePicker1.Value.Second;
//星期和微秒不用设置
SetLocalTime(ref st);
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧