I'm attempting to create a simple WinForms application written in C# that:
- Takes user input from a text box.
- Uses the data from that text box, and stores it in a
user_command
variable, which is then used in a method that runs the string in that variable in windows cmd.
E.g: I enter "calc.exe" in the text box. The program then passes that into cmd, therefore opening the calculator.
I am unaware of how I use that data from that text-box and put it into System.Diagnostics.Process.Start("CMD.exe", user_command);
I will leave all the code below:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace gui_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string user_command = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e, string user_command)
{
System.Diagnostics.Process.Start("CMD.exe", user_command);
}
}
}
The errors I get are:
Error CS0123 No overload for 'textBox1_TextChanged' matches delegate 'EventHandler' gui_1
CodePudding user response:
The problem here is you put a third parameters on your textBox1_TextChange eventhandler which causing you the error.
I suggest you put the System.Diagnostics.Process.Start("CMD.exe", user_command);
on to the button1_Click event, and remove the 3rd parameter on your textBox1_TextChange.
When you click the button1 it will run the CMD.exe
with the user_command
as argument.
namespace gui_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string user_command = textBox1.Text;
System.Diagnostics.Process.Start("CMD.exe", user_command);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
EDIT:
executing different process with arguments.
try to type on your textbox1
before clicking the button1 c:\test.txt
.
this will try to open test.txt file on your drive c: on the notepad and ask you if you want to create the file if it is not existing.
public void button1_Click(object sender, EventArgs e)
{
string user_command = textBox1.Text;
System.Diagnostics.Process.Start("notepad.exe", user_command);
}
EDIT2:
executing CMD.exe
to execute the parameter.
/c
argument will execute the arguments entered on textbox1 upon starting the CMD.exe
public void button1_Click(object sender, EventArgs e)
{
string user_command = textBox1.Text;
System.Diagnostics.Process.Start("CMD.exe", "/c " user_command);
}
CodePudding user response:
The eventargs (e) should contain the value in the checkbox. put a breakpoint and inspect it. I am sure you will find the text in there.
Remove your third parameter to start matching the overload again !
I hope this code is for educational purposes since it will open a huge gap in the security of the server it is running on.