局域网上远程电脑的日期时间
本文关键字:日期 时间 电脑 程电脑 局域网 | 更新日期: 2023-09-27 18:29:40
为了我的应用程序需要,我想从连接LAN的服务器PC获取当前DateTime。在谷歌上搜索后,我发现了一篇MSDN文章。但是它是用vb写的。我怎么能用c#做呢?有线索吗?
谢谢。
我从另一个答案中得到了一个解决方案。它非常适合我。
try
{
string pc = "pcname";
//string domain = "yourdomain";
//ConnectionOptions connection = new ConnectionOptions();
//connection.Username = some username;
//connection.Password = somepassword;
//connection.Authority = "ntlmdomain:" + domain;
string wmipath = string.Format("''''{0}''root''CIMV2", pc);
//ManagementScope scope = new ManagementScope(
// string.Format("''''{0}''root''CIMV2", pc), connection);
ManagementScope scope = new ManagementScope(wmipath);
scope.Connect();
ObjectQuery query = new ObjectQuery(
"SELECT * FROM Win32_LocalTime");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_LocalTime instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Date: {0}-{1}-{2}", queryObj["Year"], queryObj["Month"], queryObj["Day"]);
Console.WriteLine("Time: {0}:{1}:{2}", queryObj["Hour"], queryObj["Minute"], queryObj["Second"]);
}
}
catch (ManagementException err)
{
Console.WriteLine("An error occurred while querying for WMI data: " + err.Message);
}
catch (System.UnauthorizedAccessException unauthorizedErr)
{
Console.WriteLine("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}