簡體   English   中英

使用ac#程序在命令提示符下使用其IP地址關閉另一個系統?

[英]Shutdown another system using its IP address in the command prompt by a c# program?

我正在使用一個ac#程序,該程序將從記事本文件中提取IP地址,對於每個IP地址,如果系統已打開電源,它將在命令提示符下運行一個命令,該命令將遠程關閉它。 我已經編寫了此代碼,該代碼當前正在獲取系統的IP地址(當前不在記事本文件中),檢查它的打開還是關閉狀態,並根據要求遠程關閉它。

它正在使用ping請求,但這很好,因為此處沒有系統拒絕ping請求。 我要讀取的是IP地址,如果已打開,請在用戶說“是”時將其關閉。 [我現在使用“ shutdown \\ s”關閉系統“]這是我的代碼。

        Console.WriteLine("1-Check PC Status\n2-Exit");
        input = Int32.Parse(Console.ReadLine());
        switch (input)
        {
                case 1:
                    string LocalIP = string.Empty;
                    var host = Dns.GetHostEntry(Dns.GetHostName());
                    foreach (var ip in host.AddressList)
                    {
                        if (ip.AddressFamily == AddressFamily.InterNetwork)
                        {
                            LocalIP = ip.ToString();
                        }
                    }

                    var PingRequest = new Ping();
                    var PingReply = PingRequest.Send(LocalIP);
                    if (PingReply.Status == IPStatus.Success)
                    {
                        Console.WriteLine("{0, -20} {1, 5}", "IP Address", "Status");
                        Console.WriteLine("{0, -20} {1, 5}", LocalIP, "On");
                        Console.WriteLine("\nShutdown the system (y/n)?");
                        string shutdownChoice;
                        shutdownChoice = Console.ReadLine();
                        if(shutdownChoice == "y" || shutdownChoice == "Y")
                        {
                            Process cmdProcess = new Process();
                            cmdProcess.StartInfo.FileName = "cmd.exe";
                            cmdProcess.StartInfo.RedirectStandardInput = true;
                            cmdProcess.StartInfo.RedirectStandardOutput = true;
                            cmdProcess.StartInfo.CreateNoWindow = true;
                            cmdProcess.StartInfo.UseShellExecute = false;
                            cmdProcess.Start();

                            cmdProcess.StandardInput.WriteLine("shutdown /s");
                            cmdProcess.StandardInput.Flush();
                            cmdProcess.StandardInput.Close();
                            cmdProcess.WaitForExit();
                        }
                        else
                        {
                            input = 2;
                        }
                    }
                    else
                    {
                        Console.WriteLine("{0, -20} {1, 5}", LocalIP, "Off");
                    }
                    break;
                case 2:
                    input = 2;
                    break;
                default:
                    Console.WriteLine("Incorrect input");
                    break;
            }

我知道此命令shutdown \\i但這將打開一個對話框以輸入IP地址,但是我想通過控制台直接將其關閉,而沒有任何其他對話框或窗口。 shutdown \\m //computername不能正常運行。

道歉,不勝枚舉。 謝謝

嘗試使用shutdown /s /f /m \\\\computername

如果這不起作用,我建議您檢出PsTools套件。 他們有一些命令行工具,PsShutdown可以提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM