Use Command Line with C #


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

I thought of putting a button to open a CNF file, and with another button (built-in command), and saved in CSR file on 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 through the command line, will change to myfile.cnf .


asked by anonymous 17.04.2015 / 17:52

2 answers


Here is a variation of posted by Guilherme Nascimento :

public void ExecutarComandoSSL(string arquivoCNF, string arquivoCSR) {
    using (System.Diagnostics.Process processo = new System.Diagnostics.Process()) {
        processo.StartInfo.FileName = Environment.GetEnvironmentVariable("comspec");
        processo.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        processo.StartInfo.Arguments = string.Format("/c openssl req -new -config {0} -out \"{1}\"", arquivoCNF, arquivoCSR);

        //processo.StartInfo.CreateNoWindow = true;

Note : The command will execute in the current user's desktop directory. Information .

  • Do the following, in the form, put:

    • Two buttons, one to fetch the CNF , and another to save in CSR format.
    • Place a OpenFileDialog component.
  • Create a class to retrieve and save information about the files:

    public static class VariaveisGlobais 
        public static string CNF { get; set; }
        public static string CSR { get; set; }
  • In the button responsible for searching the CNF file, place:

    DialogResult resposta = openFileDialog1.ShowDialog();
    if (resposta == DialogResult.OK) {
         string arquivo = openFileDialog1.FileName;
         VariaveisGlobais.CNF = arquivo;
         VariaveisGlobais.CSR = "cert.csr"; // Leia a sugestão

    Hint : You can use the SaveFileDialog component to allow the user to save the file to location of your choice.

  • In the CSR button, save the file:

    string arquivoCNF = VariaveisGlobais.CNF;
    string arquivoCSR = VariaveisGlobais.CSR;
    // Aqui você poderia tratar o conteúdo das variáveis
    ExecutarComandoSSL(arquivoCNF, arquivoCSR);
  • 22.04.2015 / 19:11

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

    You will have to join the command cd with openssl using & , 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;

    For simplicity you can pass the arguments to Start() , as is the response from SOen :

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