通过 C# 程序在命令提示符下使用其 IP 地址关闭另一个系统?
Shutdown another system using its IP address in the command prompt by a c# program?
我正在开发一个 c# 程序,该程序将从记事本文件中获取 IP 地址,对于每个 IP 地址,如果系统已开机,它会 运行 命令提示符下的命令远程关闭它。我写了这段代码,目前正在获取我系统的 IP 地址(目前不是通过记事本文件),检查它是打开还是关闭,并根据要求远程关闭它。
它正在使用 ping 请求,但这很好,因为这里没有系统拒绝 ping 请求。我想要的是读取 IP 地址,如果它打开,如果用户说 "Yes",则将其关闭。 [我现在已经使用 "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 地址,但我想直接通过控制台将其关闭而无需任何其他对话框或 window。 shutdown \m //computername
效果不佳。
对于冗长的描述,我们深表歉意。
谢谢
尝试使用 shutdown /s /f /m \computername
如果这不起作用,我建议您查看 PsTools 套件。他们有一些命令行工具,PsShutdown 可以提供帮助。
我正在开发一个 c# 程序,该程序将从记事本文件中获取 IP 地址,对于每个 IP 地址,如果系统已开机,它会 运行 命令提示符下的命令远程关闭它。我写了这段代码,目前正在获取我系统的 IP 地址(目前不是通过记事本文件),检查它是打开还是关闭,并根据要求远程关闭它。
它正在使用 ping 请求,但这很好,因为这里没有系统拒绝 ping 请求。我想要的是读取 IP 地址,如果它打开,如果用户说 "Yes",则将其关闭。 [我现在已经使用 "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 地址,但我想直接通过控制台将其关闭而无需任何其他对话框或 window。 shutdown \m //computername
效果不佳。
对于冗长的描述,我们深表歉意。 谢谢
尝试使用 shutdown /s /f /m \computername
如果这不起作用,我建议您查看 PsTools 套件。他们有一些命令行工具,PsShutdown 可以提供帮助。