简体   繁体   中英

c# Reboot Remote Machine, hostname is textbox

Im really new to c# and i think i might have an issue with my program. So i want the program to run, it has 1 button to reboot the remote machine, and a textbox to enter in the machine name (windows xp sp3 environment). This is the code that i have and its not rebooting the machine. i do not want to use psexec unless i can call psexec from the program itself b/ci want this to be a dynamic program. long story short its bringing up the cmd prompt but for some reason the machine is not rebooting (i have permissions on the remote machine)... Any suggestions would be greatly appreciated since its prob something simple that i overlooked.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnReboot_Click(object sender, EventArgs e)
        {
            string hostname;
            hostname = textBox1.Text;
            Process.Start("shutdown", "-r -f \\\\"+ hostname);
        }
    }
}

我不知道你们遇到了什么问题,但是可能是“访问被拒绝”,如果您尝试在远程计算机上访问c $,则可以获取访问权限,并且他们执行您正在执行的过程:

net use \\computername\c$ password /USER:administrator

You can try this:

var process = new Process();
var startInfo = new ProcessStartInfo
{
     FileName = "cmd.exe",
     Arguments = "/c\"" + string.Format("shutdown /m \\\\{0} /f /t 00 /r", textBox1.Text) + "\""
};
process.StartInfo = startInfo;
process.Start();

Rather than executing shutdown directly it launches a prompt and executes the command in the prompt, like you would do manually.

You can add

WindowStyle = ProcesswindowStyle.Hidden

to the ProcessStartInfo initializer to hide the DOS windows popping up.

Shouldnt it be ?

"shutdown", "/r /f /m \\\\" + hostname

Try

Process.Start("shutdown", "-r -f -m \\\\\"+ hostname); 

-m is the switch for a remote machine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM