How to cancel an execution of a command window in C#? - c#

So I'm using visual studio to run a command line to get a "dir /s >
c:\log.txt".
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 = "/C dir " + drivePath + " /s > c:\\log.txt";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
I'm wanting to put this in a backgroundWorker so that I'm able to cancel it if the process is taking too long. But I don't see any way to cancel the execution of command line since the execution of the command is outside the scope of the program. Is there some logic I can add to this so that it will at least act as if I've cancelled it?

Related

How to stop the cmd window from closing

I run PsExec in a WPF application, but after execution, the window closes.
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\SysWoW64\PsExec64.exe";
process.StartInfo.Arguments = String.Format(#"\\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
I also tried:
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\SysWoW64\PsExec64.exe";
process.StartInfo.Arguments = String.Format(#" \K \\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
but nothing happens here. A window appears only for a second.
How can I stop the window from closing? Why doesn't "WaitForExit" do it?
Try running:
Process process = new Process();
process.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
process.StartInfo.Arguments = String.Format(#"/k C:\Windows\SysWoW64\PsExec64.exe \\{0} ipconfig", TextBox_PCin.Text);
process.Start();
process.WaitForExit();
WaitForExit did not work for you because PsExec64.exe does not wait for user input at all. It take commands as an argument >> parse & run it >> exit process. So technically your "code" did wait for PsExec64.exe to exit and then continued.

CMD log to file not working for Geth.exe

I'm using the below code to log output of a cmd call to a file however it's not working at times.
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 = "/C dir C:\\ >c:\\temp\\dir.txt";
startInfo.Arguments = "/C \"C:\\Program Files\\Geth\\geth.exe\" --exec \"web3.eth.getBalance(web3.eth.accounts[0]);\" attach >c:\\temp\\out.txt";
process.StartInfo = startInfo;
process.Start();
The simple dir works fine.
Using the Ethereum geth.exe without --exec works fine.
However once I include the --exec argument the output is blank. Both commands work fine and produce output if manually called in cmd.exe.
"C:\Program Files\Geth\geth.exe" attach >c:\temp\out.txt
"C:\Program Files\Geth\geth.exe" --exec "web3.eth.getBalance(web3.eth.accounts[0]);" attach >c:\temp\out.txt
I see you found a workaround, but for others:
process.Start();
process.WaitForExit();
You have to wait for the process to exit.

Running a program through C# with cmd doesn't work

I'm trying to run JMeter through C# with cmd but it just opens cmd and doesn't run anything.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory = "D:";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/k D:\\jmeter\\apache-jmeter-2.13\\bin\\ApacheJMeter.jar -n -t D:\\Delo\\dokument.jmx";
process.StartInfo = startInfo;
process.Start();
That code just opens the cmd and nothing happens. I've tried changing the working directory but it doesn't work. If I don't set the working directory, cmd just open at my debug directory. This does work if I start it directly from cmd (without C#).
Solved with this: a link
I'm not exactly sure what are you trying to achieve, and why you aren't using System.Diagnostics ? But I have a suggestion if I understood you right:
> System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
> startInfo.WorkingDirectory = "D:";
> startInfo.FileName = #"D:\jmeter\apache-jmeter-2.13\bin\ApacheJMeter.jar";
> startInfo.Arguments = "";
> System.Diagnostics.Process.Start(startInfo);
>
> System.Diagnostics.ProcessStartInfo startInfo2 = new System.Diagnostics.ProcessStartInfo();
> startInfo2.WorkingDirectory = "D:";
> startInfo2.FileName = #"D:\Delo\dokument.jmx";
> startInfo2.Arguments = "";
> System.Diagnostics.Process.Start(startInfo2);
I don't think you will be able to run .jar file directly via cmd interpreter, go for the following alternatives:
use jmeter.bat wrapper script
call Java executable like: path\to\java.exe -jar D:\\jmeter\\apache-jmeter-2.13\\bin\\ApacheJMeter.jar ...
I would also suggest using -l command line argument so .jtl results file could be generated.
See How Do I Run JMeter in Non-GUI Mode? article for details. I also believe that Full list of command-line options will be helpful in your case.

Java Keytool and C#

I'm trying to make a program in C# to run an automatic key tool and generate a key for Android development but I have some problems that I can't figure out.
Code in C#:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo =
new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName =
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
+ "\\Java\\jdk1.7.0_40\\bin\\keytool.exe";
startInfo.Arguments =
"-exportcert -alias androidkey -keystore d:\\debug.keystore > C:\\asd.txt";
process.StartInfo = startInfo;
process.Start();
On the command line it tells me this:
Illegal option: >keytool -exportcert [OPTION] ...
And I tried to run the exact command in the key tool manually and there was no problem.

sending command to cmd through c#

I want to execute a command to lock the drive through bit locker when button is clicked. How to do this? I'm new in c#
The command is:
manage-bde -lock x:
How it will be send to console? here is the code
private void btnlock_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 = "/C manage-bde -lock "+textBox1.Text+":";
process.StartInfo = startInfo;
process.Start();
}
You can use the Process class in System.Diagnostics namespace.
It should be something like this:
System.Diagnostics.Process.Start("manage-bde", "-lock x:");
The command isn't being executed because your command line doesn't know where to find the manage-bde program.
All you need to do is add the full path of the file like so:
startInfo.Arguments = #"/C C:\Program Files\Foo\manage-bde.exe -lock "+textBox1.Text+":";
Note: I'm not sure if the .exe part is necessary, but it doesn't hurt adding it in. Also, make sure you either use 2 backslashes (\\) or use the # before the quotation mark at the start.

Categories