客戶的機器的系統時間經常出錯,導致給他們做的軟體無法正常使用,所以後來就加了乙個同步網路時間的小功能。實現起來很簡單,但是卻很使用。
這個小功能就是先獲取網路時間,然後將系統的時間修改成從網路獲得的時間。下面是具體的實現:
usingsystem;
using
system.collections.generic;
using
system.linq;
using
system.text;
using
system.io;
using
system.net;
using
system.net.sockets;
using
system.text.regularexpressions;
using
system.runtime.interopservices;
using
system.runtime;
//////
網路時間
///
public
class
nettime
}string temparray = html.split(';'
);
for (int i = 0; i < temparray.length; i++)
string year = temparray[1].split('
=')[1
];
string month = temparray[2].split('
=')[1
];
string day = temparray[3].split('
=')[1
];
string hour = temparray[5].split('
=')[1
];
string minite = temparray[6].split('
=')[1
];
string second = temparray[7].split('
=')[1
]; dt = datetime.parse(year + "
-" + month + "
-" + day + "
" + hour + "
:" + minite + "
:" +second);
}catch
(webexception)
catch
(exception)
finally
return
dt; }
}
獲取網路時間,返回乙個datetime物件,然後傳給設定系統時間的方法,修改系統時間。
usingsystem;
using
system.collections.generic;
using
system.linq;
using
system.text;
using
system.io;
using
system.net;
using
system.net.sockets;
using
system.text.regularexpressions;
using
system.runtime.interopservices;
using
system.runtime;
//////
更新系統時間
/// public
class
updatetime
//////
設定系統時間
/// ///
需要設定的時間
///返回系統時間設定狀態,true為成功,false為失敗
public
static
bool
setdate(datetime dt)
}
兩個方法分別寫在了兩個類裡面,只需要在客戶端例項化兩個物件,然後依次呼叫其方法即可,簡單實用。
ps:win8修改系統時間需要管理員的許可權,下篇部落格介紹如何讓程式預設以管理員許可權執行,敬請期待!
Linux同步網路時間
linux伺服器執行久時,系統時間就會存在一定的誤差,一般情況下可以使用date命令進行時間設定,但在做資料庫集群分片等操作時對多台機器的時間差是有要求的,此時就需要使用ntpdate進行時間同步 安裝 yum install ntpdate ntpdate簡單用法 ntpdate ip ntpda...
linux同步網路時間
linux伺服器執行久時,系統時間就會存在一定的誤差,一般情況下可以使用date命令進行時間設定,但在做資料庫集群分片等操作時對多台機器的時間差是有要求的,此時就需要使用ntpdate進行時間同步。一 修改時區 cp usr share zoneinfo asia shanghai etc loca...
Linux同步網路時間
linux伺服器執行久時,系統時間就會存在一定的誤差,一般情況下可以使用date命令進行時間設定,但在做資料庫集群分片等操作時對多台機器的時間差是有要求的,此時就需要使用ntpdate進行時間同步 安裝 yum install ntpdate ntpdate簡單用法 ntpdate ip ntpda...