Reputation: 879
So I am trying to use a Button
to open a CMD.exe
window to execute a command which allows me to automatically open a Remote Desktop with an IP from my network. I already built a piece of code, but it doesn't work. This is the code:
private void cmdRemote_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "mstsc /v:" + txtIP.Text;
process.StartInfo = startInfo;
process.Start();
}
I would like to know why it doesn't execute the command.
Upvotes: 6
Views: 8919
Reputation: 1685
Instead of calling the cmd.exe, you can call the mstsc
startInfo.FileName = "mstsc"
startInfo.Arguments= " /v " + txtIP.Text
Upvotes: 0
Reputation: 17055
Add "/c" in front of your arguments list, else cmd won't execute it:
cmd /c mstsc /v:...
Or, why don't you call mstsc
directly?
private void cmdRemote_Click(object sender, EventArgs e)
{
var process = new System.Diagnostics.Process();
process.StartInfo = new ProcessStartInfo
{
FileName = "mstsc"
Arguments = "/v:" + txtIP.Text
}
process.Start();
}
or shorter:
Process.Start("mstsc", "/v:" + this.txtIP.Text);
Don't forget to validate the value of Text
!
Upvotes: 5