如何在C#控制台应用程序中获取另一个(远程)服务器的TimeZone。 [英] How do I get TimeZone of another (Remote) Server in C# console application.
本文介绍了如何在C#控制台应用程序中获取另一个(远程)服务器的TimeZone。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在C#控制台应用程序中获取另一个(远程)服务器的TimeZone,任何人都知道如何获取远程机器的TimeZone? br />
请帮忙。
谢谢!
Hi,
I am trying to get TimeZone of another (Remote) Server in C# console application, anyone has idea how to get TimeZone of remote machine?
Please help.
Thanks!
推荐答案
已经在这里描述了
http://www.c-sharpcorner.com/uploadfile/vendettamit/setting-your-website-datetime-according-to-time-客户区域偏移/ [ ^ ]
its already described here
http://www.c-sharpcorner.com/uploadfile/vendettamit/setting-your-website-datetime-according-to-time-zone-offset-of-the-client/[^]
我昨晚刚遇到这个因为我需要同一件事情。我使用WMI查询来获得我需要的东西。您需要将System.Management.dll添加到您的引用,然后使用以下代码:
I just came across this last night because I needed the same thing. I used a WMI query to get what I needed. You'll need to add System.Management.dll to your references, then use the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Data;
namespace DeleteMeConsole
{
class Program
{
static void Main(string[] args)
{
bool trusted = true;
if (trusted)
{
GetTimeZone("", "", "myServer"); //trusted connection to myServer
}
else
{
GetTimeZone("myUserName", "myPassword", "myServer"); //standard connection to myServer
}
}
private static void GetTimeZone(string loginName,string userPassword, string serverName)
{
try
{
ManagementScope Scope;
if (loginName.Trim().Length > 0)
{
//log in name has been provided, so this is not a trusted connection. Build the connection options and set values
ConnectionOptions Conn = new ConnectionOptions();
Conn.Username = loginName;
Conn.Password = userPassword;
Conn.Authority = "ntlmdomain:DOMAIN";
if (Conn.Username.IndexOf(@"\") > 0)
{
//domain name was passed in. Ensure that gets added to the connection options
int delimiterLocation = loginName.IndexOf(@"\");
string domainName = loginName.Substring(0, delimiterLocation);
string eventUserName = loginName.Substring(delimiterLocation + 1);
Conn.Username = eventUserName;
Conn.Authority = "ntlmdomain:" + domainName;
}
//build the scope with the server name and connection options
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", serverName), Conn);
}
else
//this is a trusted connection. The conn object need not be passed
Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", serverName), null);
//connect, pass the query and instantiate the searcher
Scope.Connect();
ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_TimeZone");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
foreach (ManagementObject WmiObject in Searcher.Get())
{
Console.WriteLine("{0,-35} {1,-40}", "Bias", WmiObject["Bias"]);// Sint32
Console.WriteLine("{0,-35} {1,-40}", "Caption", WmiObject["Caption"]);// String
Console.WriteLine("{0,-35} {1,-40}", "DaylightBias", WmiObject["DaylightBias"]);// Sint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightDay", WmiObject["DaylightDay"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightDayOfWeek", WmiObject["DaylightDayOfWeek"]);// Uint8
Console.WriteLine("{0,-35} {1,-40}", "DaylightHour", WmiObject["DaylightHour"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightMillisecond", WmiObject["DaylightMillisecond"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightMinute", WmiObject["DaylightMinute"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightMonth", WmiObject["DaylightMonth"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightName", WmiObject["DaylightName"]);// String
Console.WriteLine("{0,-35} {1,-40}", "DaylightSecond", WmiObject["DaylightSecond"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "DaylightYear", WmiObject["DaylightYear"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "Description", WmiObject["Description"]);// String
Console.WriteLine("{0,-35} {1,-40}", "SettingID", WmiObject["SettingID"]);// String
Console.WriteLine("{0,-35} {1,-40}", "StandardBias", WmiObject["StandardBias"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardDay", WmiObject["StandardDay"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardDayOfWeek", WmiObject["StandardDayOfWeek"]);// Uint8
Console.WriteLine("{0,-35} {1,-40}", "StandardHour", WmiObject["StandardHour"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardMillisecond", WmiObject["StandardMillisecond"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardMinute", WmiObject["StandardMinute"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardMonth", WmiObject["StandardMonth"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardName", WmiObject["StandardName"]);// String
Console.WriteLine("{0,-35} {1,-40}", "StandardSecond", WmiObject["StandardSecond"]);// Uint32
Console.WriteLine("{0,-35} {1,-40}", "StandardYear", WmiObject["StandardYear"]);// Uint32
}
}
catch (Exception e)
{
Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
}
Console.WriteLine("Press Enter to exit");
Console.Read();
}
}
}
这篇关于如何在C#控制台应用程序中获取另一个(远程)服务器的TimeZone。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文