Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted
Updated 12-Mar-14 15:54pm
v2

 
Share this answer
 
Comments
Member Nitin Patil 13-Mar-14 8:12am    
Hi Andrew,

The article link is helpful for web application but I am looking for how to get TimeZone of remote machine C# console application?

Thanks!
AndrewCharlz 13-Mar-14 8:21am    
http://www.c-sharpcorner.com/UploadFile/mahesh/TimeZoneSample12142005103321AM/TimeZoneSample.aspx

try this i have not tried yet
Member Nitin Patil 13-Mar-14 9:49am    
Hi Andrew,

This link shows how to get timezone from curreunt server (where application is hosted) but I am looking for TimeZone of remote machine in C# console application.

Thanks!

Thanks!
AndrewCharlz 13-Mar-14 13:29pm    
how do u connect ur remote server is it using the ip
Member Nitin Patil 13-Mar-14 16:49pm    
I have server full name/IP only. Not sure how to connect and get the TimeZone?

like .Net provided "System.ServiceProcess" to get services infromation of any machine using machine name. I am trying to find, is there any Namespace/class available that will provide me TimeZone information of any server using full machine name/IP.
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();
        }
    }
}
 
Share this answer
 
Comments
Member 13245133 22-Nov-17 10:39am    
It gives error on Scope.Connect()
The error reads, "{"The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"}"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900