C# 从客户端重新启动远程服务器

C# 从客户端重新启动远程服务器,c#,asp.net-mvc-4,visual-studio-2013,C#,Asp.net Mvc 4,Visual Studio 2013,我已经访问了远程服务器,但可能有问题。因此,我想使用c#通过客户端重新启动远程。可以重新启动吗 编辑:有关更合理的方法,请参见下文 是的,这是可能的 首先,使用namespace语句添加以下内容: using System.Diagnostics; using System.Runtime.InteropServices; 要关闭计算机,请使用: Process.Start("shutdown","/s /t 0"); // starts th

我已经访问了远程服务器,但可能有问题。因此,我想使用c#通过客户端重新启动远程。可以重新启动吗

编辑:有关更合理的方法,请参见下文
是的,这是可能的

首先,使用namespace语句添加以下内容:

using System.Diagnostics;
using System.Runtime.InteropServices; 
要关闭计算机,请使用:

Process.Start("shutdown","/s /t 0");    // starts the shutdown application 
                                        // the argument /s is to shut down the computer
                                        // the argument /t 0 is to tell the process that 
                                        // the specified operation needs to be completed 
                                        // after 0 seconds
Process.Start("shutdown","/r /t 0"); // the argument /r is to restart the computer
要重新启动计算机,请使用:

Process.Start("shutdown","/s /t 0");    // starts the shutdown application 
                                        // the argument /s is to shut down the computer
                                        // the argument /t 0 is to tell the process that 
                                        // the specified operation needs to be completed 
                                        // after 0 seconds
Process.Start("shutdown","/r /t 0"); // the argument /r is to restart the computer

来源:

这是我的解决方案,它支持静默模式、“启动并忘记”和延迟重启。在中,只需为流程启动单独登录即可增强

public static bool RebootRemoteMachineSOVersion(ContentControl parentControl, string remoteHostNameOrIp, int waitSeconds = 60, bool silent = false, bool waitForExit = true)
{
    waitSeconds = Math.Max(0, waitSeconds);

    if (!silent && MessageBox.Show($"Reboot remote computer ({ remoteHostNameOrIp }) in { waitSeconds } seconds?", "Reboot remote machine", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
    {
        return false;
        //<-----------
    }

    ProcessStartInfo processInfo = new ProcessStartInfo();
    processInfo.FileName = "shutdown.exe";
    processInfo.Arguments = $@"-r -t { waitSeconds } -m \\{ remoteHostNameOrIp }";
    processInfo.WindowStyle = ProcessWindowStyle.Hidden;
    processInfo.CreateNoWindow = true;

    Process proc;
    try
    {
        proc = Process.Start(processInfo);

        if (waitForExit) proc.WaitForExit();
        else return true;
        //<----------
    }
    catch (Exception ex)
    {
        if (!silent) MessageBox.Show($"An error happened:\n\n{ ex.Message }", "Reboot remote machine", MessageBoxButton.OK, MessageBoxImage.Error);
        return false;
        //<-----------
    }

    {
        string message = "";
        const int ERROR_BAD_NETPATH = 53;
        const int ERROR_SHUTDOWN_IN_PROGRESS = 1115;
        const int RPC_S_UNKNOWN_IF = 1717;

        switch (proc.ExitCode)
        {
            case 0:
                if (!silent) MessageBox.Show($"Remote computer is rebooting  ({ remoteHostNameOrIp }) in { waitSeconds } seconds.", "Reboot remote computer", MessageBoxButton.OK, MessageBoxImage.Information);
                return true;
            //<----------

            case ERROR_BAD_NETPATH:
                message = $"Remote computer not found ({ remoteHostNameOrIp })";
                break;

            case ERROR_SHUTDOWN_IN_PROGRESS:
                message = $"A shutdown is already in progress ({ remoteHostNameOrIp })";
                break;

            case RPC_S_UNKNOWN_IF:
                message = $"Remote computer does not accept shutdown. Probably it is currently booting. ({ remoteHostNameOrIp })";
                break;

            default:
                message = $"Could not shut down - errorcode: { proc.ExitCode } ({ remoteHostNameOrIp })";
                break;
        }

        if (!silent) MessageBox.Show($"{ message }", "Reboot remote computer", MessageBoxButton.OK, MessageBoxImage.Error);

        return false;
    }
}
public static bool rebootortemachineversion(ContentControl parentControl,string remoteHostNameOrIp,int-waitSeconds=60,bool-silent=false,bool-waitForExit=true)
{
waitSeconds=Math.Max(0,waitSeconds);
如果(!silent&&MessageBox.Show($“在{waitSeconds}秒内重新启动远程计算机({remoteHostNameOrIp})”,“重新启动远程计算机”,MessageBoxButton.YesNo,MessageBoxImage.Question,MessageBoxResult.No)=MessageBoxResult.No)
{
返回false;

//你想用C#重新启动它还是仅仅重新启动它?什么是客户端操作系统和服务器操作系统?你如何访问它?你的问题的答案是响亮的“是!”今天买一只小猫!请澄清你的意思?可能提供代码示例…好的。使用C#如何做到这一点?@Karthic这应该是公认的答案。这一个满足了远程重启的需要。而且它实际上使用C#来执行实际重启。这里没有反模式。Amit,我唯一的建议是添加对您答案的描述,而不是答案只是一个代码块。@Karthic如果这有帮助,请寻求和/或接受答案这不仅是从c#执行重新启动的可怕方式(Process.Start()以这种方式使用是一种反模式),而且无法回答OP“从客户端重新启动远程服务器”的需要我假设他用c#编写了两个应用程序(服务器和客户端应用程序),但他没有提到如何连接到远程服务器