获取远程PC的日期时间? [英] Get remote PC's date time?

查看:191
本文介绍了获取远程PC的日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何类可以在.net中获取远程PC的日期时间?为了做到这一点,我可以使用电脑名称或时区。对于每种情况,有不同的方式来获取当前的日期时间?我正在使用Visual Studio 2005。

解决方案

我给你一个使用 WMI 。您可能需要或可能不需要域名和安全性信息:

  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} \\\\\\CIMV2,pc),连接);
ManagementScope scope = new ManagementScope(wmipath);
scope.Connect();

ObjectQuery query = new ObjectQuery(
SELECT * FROM Win32_LocalTime);

ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope,query);

foreach(在Searchers.Get()中的ManagementObject queryObj)
{
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(查询WMI数据时发生错误:+ err.Message);
}
catch(System.UnauthorizedAccessException unauthorizedErr)
{
Console.WriteLine(连接错误(用户名或密码可能不正确):+ unauthorizedErr.Message);
}


Is there any class available to get a remote PC's date time in .net? In order to do it, I can use a computer name or time zone. For each case, are there different ways to get the current date time? I am using Visual Studio 2005.

解决方案

I give you a solution which uses WMI. You may or may not need the domain and security information:

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);
}

这篇关于获取远程PC的日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆