Use Command Line with C#

Asked

Viewed 1,471 times

10

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

I thought I’d put a button to open a file CNF, and with another button (embeds the command), and saved in file CSR 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 modify to Myfile.cnf.

Suggestions?

  • 1

    Is this necessary? . Net has a complete infrastructure that even exceeds what Openssl does in a well-integrated way with the entire ecosystem.

  • Sorry if I’m too dumb, but I can’t understand how this can help someone to work with Openssl.

  • 2

    Could you explain exactly what process flow your tool should have? I feel something is missing.

  • @Victorstafusa in my workplace, there are women who do not know how to use Openssl. What I wanted to do was this: A Windows Forms, with 2 buttons. The first serves to fetch the file "csr" and the second button is to save the file in "cnf". But for that, Openssl would have to be running, without people noticing, understood?

  • @mustache some help?

  • What is your question/problem anyway? What did you try?

Show 1 more comment

3 answers

6

You want to do the same procedure cmd within an application written in c#? If this is it, then you can use System.Diagnostics.Process.

You will have to unite the command cd with the 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();

To simplify you can pass the arguments by Start(), as per this response from Soen:

const string strCmdText = "/C cd Desktop& openssl \"myfile.csr\" -req -new -config myfile.cnf out.";
...
process.Start("CMD.exe", strCmdText);
  • Amigo @Guilhermenascimento, helps a lot, but "Myfile.csr" has to change, depending on the name of the file. Is it possible? I edited the formula.

  • @Godfathersantana You can format the string with the .Format(). Something like: startInfo.Arguments = "/C cd Desktop& openssl \"{0}\" -req -new -config myfile.cnf out.".format(variavelCSR);.

  • @qmechanik, I don’t understand where I’m gonna put this line of code. Since I’ll be retrieving the file with a button, the code Guilherme Nascimente left me is for the button save?

  • 1

    @Godfathersantana thought his difficulty was with the command line, but I see now that you are having difficulty in developing the whole (from selecting the file to passing it to the command line), so have a while trying to post a functional example.

  • Thank you, I’ll keep @Guilhermenascimento.

3


Follows a variant of the code 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;
        processo.Start();
        processo.WaitForExit();
    }
}

Note: The command will run in the current user’s desktop directory. Information.

  1. Do the following, on the form, put:

    • Two buttons, one to fetch the file CNF, and another to save in format CSR.
    • Put a component OpenFileDialog.
  2. Create a class to rescue and save file information:

    public static class VariaveisGlobais 
    {
        public static string CNF { get; set; }
        public static string CSR { get; set; }
    }
    
  3. On the button responsible for fetching the file CNF, place:

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

    Suggestion: You can use the component SaveFileDialog to allow the user to save the file in the preferred location.

  4. On the button responsible for saving the file in format CSR, place:

    string arquivoCNF = VariaveisGlobais.CNF;
    string arquivoCSR = VariaveisGlobais.CSR;
    
    // Aqui você poderia tratar o conteúdo das variáveis
    ExecutarComandoSSL(arquivoCNF, arquivoCSR);
    
  • Amigo @qmechanik, I made the program with this code above indicated, but the.StartInfo.Arguments process is not sending the content to the Command Line. I can send you the program?

  • @Godfathersantana Can you elaborate more on what’s going on? I didn’t get to test the code, try changing the /C for /K in the line where the arguments are defined.

  • @Godfathersantana Managed to debug what is happening?

  • 1

    Friend @qmechanik, I haven’t been able to, as I’ve been having a lot of work. This coming week, I will try to find time.

  • Amigo @qmechanik is not working...

  • @Godfathersantana What exactly happened? I removed one " the more I had in the function code ExecutarComandoSSL, also check the syntax where you pass the arguments, if it is correct.

  • Working what wanted friend @qmechanik, thank you!

  • @Godfathersantana If possible mark the answer as accepted. =)

Show 3 more comments

0

I know it’s late, but I saw the topic and found it interesting, because I went through the same yesterday And for those who are going through the same can solve as follows:

string executavelSSL = "\"C:\\Program Files\\OpenSSL-Win64\\bin\\openssl.exe\"";

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true; // Não preciso
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();

cmd.StandardInput.WriteLine("cd " + "ponha aqui seu caminho");
cmd.StandardInput.WriteLine(executavelSSL + " -req -new -config cert.cnf -out \"cert.csr\" out");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();

when the path of openssl is not in the environment variables (and even if it is), in the code I recommend to pass the path of . exe ("C:\caminho_onde_está_instalado\bin\openssl.exe ") instead of openssl 'cause I had some problems with that.

In this case the resulting command will be: "C:\Program Files\OpenSSL-Win64\bin\openssl.exe" -req -new -config cert.cnf -out "cert.csr" out;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.