Use Command Line with C#

Question:

In my work I use the Command Line, and I wanted to improve the work in my company. To do this, I thought of creating a C# program for people who don't know how to work with the command line.

I thought of putting a button to open a CNF file, and with another button (I embedded the command), and saved it in a CSR file on the Desktop.

That is, the button would open the command line and put the following formula:

cd desktop
openssl -req -new -config cert.cnf -out "cert.csr" out. 

That is, the file myfile.csr will be on the desktop, and via the command line, it will change to myfile.cnf .

Suggestions?

Answer:

Do you want to do the same procedure as cmd inside an application written in C#? If that's it, then you can use System.Diagnostics.Process .

You will have to merge the cd command with openssl using the & , see an example:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

//Oculta cmd
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

//Chama CMD
startInfo.FileName = "cmd.exe";

//seu comando
startInfo.Arguments = "/C cd Desktop& openssl \"myfile.csr\" -req -new -config myfile.cnf out.";

process.StartInfo = startInfo;
process.Start();

For simplicity you can pass the arguments through Start() , as shown in SOen 's answer:

const string strCmdText = "/C cd Desktop& openssl \"myfile.csr\" -req -new -config myfile.cnf out.";
...
process.Start("CMD.exe", strCmdText);
Scroll to Top