How to check a Remote service running (up) or not in ASP.NET and in C# sample code?

How to check a Remote service running (up) or not in ASP.NET and in C# sample code?


Sample code in ASP.NET and C#:

string strStatus = string.Empty;
ConnectionOptions connection = new ConnectionOptions();
                String hostName = Dns.GetHostName();

                if (!hostName.Equals(“<Server Name>”))
                {
                    connection.Username = “<Domain Name>” + "\\" + “User Name”;
                    connection.Password = “Password”;
                }

                ManagementScope scope = new ManagementScope(
                    "\\\\"+ ”Server Name” +"\\root\\CIMV2", connection);
                scope.Connect();

                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Service");


                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Name"].ToString().Equals(“<Remote Service Name>”))
                    {
                        strStatus = queryObj["Status"].ToString();
                    }
                }

                if (strStatus.ToLower().Equals("running"))
                {
                     ---------------
                     ---------------
                }

No comments:

Post a Comment