Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自局域网上远程PC的日期时间_C# - Fatal编程技术网

C# 来自局域网上远程PC的日期时间

C# 来自局域网上远程PC的日期时间,c#,C#,对于我的应用程序需要,我想从连接局域网的服务器PC获取当前日期时间。谷歌搜索后,我发现了一个新的。但它是用vb编写的。我怎么能用c#做呢?有线索吗 谢谢。我从另一位同事那里得到了一个解决方案。这对我来说非常有效 try { string pc = "pcname"; //string domain = "yourdomain"; //ConnectionOptions connection = new ConnectionOptions(); //connection.Username = so

对于我的应用程序需要,我想从连接局域网的服务器PC获取当前日期时间。谷歌搜索后,我发现了一个新的。但它是用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);
}
尝试这些转换或