I have a simple sourcecode written in C .
After compiling with GCC via command :
gcc hello.c -o hello
I have file hello .
I want to use C# to run the above hello file, what should I do?
I tried the following but it didn't work.
Contents of file hello.c
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Contents in C#
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 D:\\src\\testCompileCUsingGCC\\src\\mrtbtostr\\hello";
process.StartInfo = startInfo;
process.Start();
Results after running . Error content : is not recognized as an internal or external command,
operable program or batch file
This is the result when running the compiled hello file manually
After run command : gcc hello.c -o hello.exe
Check whether the path of hello.exe is correct
If you want to see the result in cmd, change
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; to startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Related
The command line I need to execute is
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe" /Run "C:\unity\unity\MRTK Tutorial\Builds\MRTK Tutorial.sln"
This works from a windows command line without issues,
I formatted it into a string for visual studio
When running from C# this command never executes and the contents of result are ""
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("C:\\Program Files(x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\IDE\\devenv.exe", " /Run \"C:\\unity\\unity\\MRTK Tutorial\\Builds\\MRTK Tutorial.sln\"");
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
You can try the following code to use c# to execute devenv.exe.
var devEnvPath = #"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe";
string SolutionFile = #"D:\Test\testconsole\testconsole.sln";
ProcessStartInfo startInfo = new ProcessStartInfo(devEnvPath);
startInfo.Arguments = "/Run " + SolutionFile;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Console.ReadKey();
Based on my test, the above code will open vs2019 and open the startup project.
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.
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.
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.