none
Getting Windows7 Server status RRS feed

  • Question

  • Dear All,

    One of my applications checks the network server availability (i.e. using ip address or server name), the application was running fine until recently the client transfers their server to windows7. Previously, I was using Network.Ping() method to check the server availability. Can anyone please help in find its alternate solution for windows7.
    The methods I have tried but failed are as follows:

    1. My.Computer.Network.Ping()
    2. ping.Send()
    3. FileSystem.DirectoryExists() with complete directory address of server
    4. Dns.GetHostEntry()
    5. tcpClient.Connect()

    Thank you for your help.
    Thanks & Regards,
    Maverick.

    Monday, January 6, 2014 12:13 PM

Answers

  • Hi Maverick,

    I find two ways for you to try, see the following code snippets.

    First way:

    public static bool CmdPing(string strIp)

            {

                string str;

                bool flag = false;

                Process process = new Process();

                process.StartInfo.FileName = "cmd.exe";

                process.StartInfo.UseShellExecute = false;

                process.StartInfo.RedirectStandardInput = true;

                process.StartInfo.RedirectStandardOutput = true;

                process.StartInfo.RedirectStandardError = true;

                process.StartInfo.CreateNoWindow = true;

                process.Start();

                process.StandardInput.WriteLine("ping -n 1 " + strIp);

                process.StandardInput.WriteLine("exit");

                string str2 = process.StandardOutput.ReadToEnd();

                if (str2.IndexOf("(0% loss)") != -1)

                {

                    str = "connect";

                    flag = true;

                }

                else if (str2.IndexOf("Destination host unreachable.") != -1)

                {

                    str = "host unreachable";

                }

                else if (str2.IndexOf("Request timed out.") != -1)

                {

                    str = "time out";

                }

                else if (str2.IndexOf("Unknown host") != -1)

                {

                    str = "unkonw host";

                }

                else

                {

                    str = str2;

                }

                process.Close();

                return flag;

            }

    Second way:

    [DllImport("wininet.dll")]

            private static extern bool InternetGetConnectedState(out int Description, int ReservedValue);

            public static bool IsConnectedToInternet()

            {

                int num;

                return InternetGetConnectedState(out num, 0);

            }

    Hope useful to you.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 7, 2014 6:05 AM
    Moderator

All replies

  • Hi Maverick,

    I find two ways for you to try, see the following code snippets.

    First way:

    public static bool CmdPing(string strIp)

            {

                string str;

                bool flag = false;

                Process process = new Process();

                process.StartInfo.FileName = "cmd.exe";

                process.StartInfo.UseShellExecute = false;

                process.StartInfo.RedirectStandardInput = true;

                process.StartInfo.RedirectStandardOutput = true;

                process.StartInfo.RedirectStandardError = true;

                process.StartInfo.CreateNoWindow = true;

                process.Start();

                process.StandardInput.WriteLine("ping -n 1 " + strIp);

                process.StandardInput.WriteLine("exit");

                string str2 = process.StandardOutput.ReadToEnd();

                if (str2.IndexOf("(0% loss)") != -1)

                {

                    str = "connect";

                    flag = true;

                }

                else if (str2.IndexOf("Destination host unreachable.") != -1)

                {

                    str = "host unreachable";

                }

                else if (str2.IndexOf("Request timed out.") != -1)

                {

                    str = "time out";

                }

                else if (str2.IndexOf("Unknown host") != -1)

                {

                    str = "unkonw host";

                }

                else

                {

                    str = str2;

                }

                process.Close();

                return flag;

            }

    Second way:

    [DllImport("wininet.dll")]

            private static extern bool InternetGetConnectedState(out int Description, int ReservedValue);

            public static bool IsConnectedToInternet()

            {

                int num;

                return InternetGetConnectedState(out num, 0);

            }

    Hope useful to you.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 7, 2014 6:05 AM
    Moderator
  • Thank you Hetro wong,

    Really appreciate your help, let me try the code n get back to you :)

    Thanks & Regards,

    Maverick.

    Tuesday, January 7, 2014 6:31 AM