确定远程系统何时重新启动

时间:2009-05-11 15:23:50

标签: c#

我正在编写一个调用批处理文件的c#程序,该文件将远程系统重新启动到另一个分区。有没有办法知道系统何时完成重启?我想知道何时重启后我可以访问远程系统。

5 个答案:

答案 0 :(得分:6)

我会ping它以确定它何时重新上线。有一个Ping内置于.net。

答案 1 :(得分:4)

当系统A告诉系统B重启时,可能它可以提供其IP地址(或其他联系信息),然后在系统Bs启动过程中,它可以使用联系信息读取文件并回叫系统A. / p>

答案 2 :(得分:0)

你有几个选择......

您可以尝试连接到其他系统上的网络服务,即:将Ping用作suggested by Jon B

但是,如果系统是您控制的系统,您还有其他选择。您可以设置计划任务(或安装服务)以在启动时启动,并让其他系统在联机时通知您。这可以防止您需要重复ping。

答案 3 :(得分:0)

我希望在周六度过一整天之前我曾经使用过这个网站。谢谢您的帮助!!以下是我最终做的事情。此代码将ping计算机并确定它何时重新联机。下一步将确定是否可以按照前面的建议打开与远程桌面端口的网络连接。随意对代码发表任何评论。

注意:以下是添加的使用声明:

使用System.Net; // 添加 使用System.Net.NetworkInformation; //添加 使用System.ComponentModel; // 添加 使用System.Threading; //已添加

    static void Main(string[] args)
    {
        string systemName = "172.30.11.148";      // Name of system beign pinged.  System name used instead of ip due to DHCP 

        Ping pingSender = new Ping();                       // Creates new ping object.  
        string data = "datadatadatadatadatadatadatadata";   // buffer of 32 bytes of data to transmit.
        byte[] buffer = Encoding.ASCII.GetBytes(data);      // buffer containing data.

        int pingTimeout = 120;          // Timeout in ms sent into pingSender.
        int maxWaitTimeout = 60;        // Maximum time to wait for system to reboot.
        int counter = 1;                // Used in while statement
        bool connectStatus = false;     // State of system connection.

        Console.WriteLine("Wait for reboot to start before trying to establish connection.");
        //Thread.Sleep(30000);            // Waits 30 seconds at start of reboot to allow network to close.
        Thread.Sleep(1000);

        try
        {
            // Performs a ping on the system.  If the system is available, while loop is exited and program
            // continues.  If the system is not available, program waits approximately 1 second and then
            // pings the system again.
            while (counter < maxWaitTimeout)
            {
                // Pings the system with 32 byts of data and waits for timeout
                Console.WriteLine("Connection attempt #: " + counter + " of " + maxWaitTimeout);
                PingReply reply = pingSender.Send(systemName, pingTimeout, buffer);
                Console.WriteLine("Status of ping to: {0} - {1}", systemName, reply.Status);

                if (reply.Status.ToString() == "Success")
                {
                    connectStatus = true;                     
                    Console.WriteLine("\nRoundTrip time: {0}", reply.RoundtripTime);
                    Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                    Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                    break;
                }

                Thread.Sleep(880);
                counter += 1;                  
            } // end while loop

            if (connectStatus == true)
            {
                Console.WriteLine("\nAble to establish connection to: {0}", systemName);
            }
            else
            {
                Console.WriteLine("\nUnable to establish network connection using ping to: {0}", systemName);
            }
        } // end try

        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }

        Console.ReadLine();     // Pause console

    } // end main   

答案 4 :(得分:0)

我还想提一下,当我研究使用“ping”时,我发现了这个链接: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx