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.
Related
I have to run a .bat file from c#...
I use this method.
file = "C:\\Diego\\PublishCore\\Startup_service.bat";
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = file;
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);
.BAT is executed... but the action I ask to perfom it does not execute...
If my .bat says MKDir MyDir... Its creates a Directory called MyDIr with no problems.
But when my bat says dotnet myApp.dll, a cmd Windows opens and closes, but it does not start myApp aplication....
If a doublé-click my .bat is runs fine.
What I am missing? Why the aplication does not start?
I solved it...
The problem was that, as my bat run the instruction dotnet myApp.dll.
I set the path file where the file was, but it was executed in the location where the my Solution is, instead of running in the same directory where I have .bat file.
I have to set WorkingDirectory and Arguments
C:\\Diego\\PublishCore\\Startup_InomCore.bat
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = "C:\\Diego\\PublishCore";
// psi.CreateNoWindow = true;
psi.FileName = #"cmd.exe";
psi.Arguments = "/c start /wait " + "C:\\Diego\\PublishCore\\Startup_InomCore.bat";
// psi.UseShellExecute = true;
psi.Verb = "runas";
var process = Process.Start(psi);
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.
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?
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.
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.