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/c i 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);
}
}
}
Shouldnt it be ?
"shutdown", "/r /f /m \\" + hostname
Try
Process.Start("shutdown", "-r -f -m \\\\\"+ hostname);
-m is the switch for a remote machine
I don't know what issues you guys are having, but it probably is "access denied", you can gain access if you try to get to c$ on the remote machine, and them do the procedure you are doing:
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.
精彩评论