Return Result of an IF executed by Command Prompt(CMD) in C#

Asked

Viewed 455 times

2

Hello, I’m trying to get a return from an IF line from Command Prompt (CMD) with Visual Studio 2015 in C#, but I’m not getting it.

The following Code, runs the CMD, inserts the directory path that contains a C++ file (.cpp) and using the Mingw compiler, compiles and generates the executable (.exe) or error message. Using IF EXIST, I confirm whether the executable was created or not. But the return of the IF that indicates whether the file was found (found) or not (not found), failing to return the result in which, will be used in a variable that will in the future serve as validation.

using System;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExecutar_Click(object sender, EventArgs e)
        {            
            string aspasDuplas = lblAspasDuplas.Text;
            string retornar;

            ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
            processStartInfo.RedirectStandardInput = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.UseShellExecute = false;
            processStartInfo.CreateNoWindow = true; // Não mostrar janela do cmd        

            Process process = Process.Start(processStartInfo);
            process.StandardInput.WriteLine(@"cd C:\\Users\\Desktop\\ConsoleApplication1\\ConsoleApplication1");
            process.StandardInput.WriteLine(@"g++ -o Source Source.cpp");
            process.StandardInput.WriteLine(@"IF EXIST " + aspasDuplas + "Source.exe" + aspasDuplas + " (ECHO found) ELSE (ECHO not found)");

            retornar = process.StandardOutput.ReadLine();

            process.StandardInput.WriteLine(@"exit");
            process.WaitForExit(); // espera o cmd.exe terminar

            txtRetorno.Text = retornar;
        }
    }
}

But your Return text is Always:

Microsoft Windows [versÆo 10.0.16299.248]

Using the Readtoend command instead of Readline:

Process process = Process.Start(processStartInfo);
process.StandardInput.WriteLine(@"cd C:\\Users\\Desktop\\ConsoleApplication1\\ConsoleApplication1");
process.StandardInput.WriteLine(@"g++ -o Source Source.cpp");
process.StandardInput.WriteLine(@"IF EXIST " + aspasDuplas + "Source.exe" + aspasDuplas + " (ECHO found) ELSE (ECHO not found)");            
process.StandardInput.WriteLine(@"exit");

retornar = process.StandardOutput.ReadToEnd();

process.WaitForExit(); // espera o cmd.exe terminar

txtRetorno.Text = retornar;

But your return is all CMD writing:

Microsoft Windows [versÆo 10.0.16299.248]
(c) 2017 Microsoft Corporation. Todos os direitos reservados.

C:\Users\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug>cd C:\\Users\\Desktop\\ConsoleApplication1\\ConsoleApplication1

C:\Users\Desktop\ConsoleApplication1\ConsoleApplication1>g++ -o Source Source.cpp

C:\Users\Desktop\ConsoleApplication1\ConsoleApplication1>IF EXIST "Source.exe" (ECHO found) ELSE (ECHO not found)
not found

C:\Users\Desktop\ConsoleApplication1\ConsoleApplication1>exit

If anyone can help me get just the IF return (found or not found). Thank you in advance for those who help.

  • 3

    This is one of the most insecure things I’ve ever seen in my life.

  • Why not make a File.Exists right after the process.WaitForExit()? As long as you know the file path, it is possible. However, I agree with @Maniero, it is something that is quite unsafe to do.

  • It’s just that I’m doing it for academic use, it’s just the code part of my TCC project

  • So, if you remove the file after you finish the process, you can?

  • 1

    It worked here, thanks for the help. Can you put your comment as a response? And if you need something I can do in this post, just say.

  • Done :)! Happy to have helped!

Show 1 more comment

1 answer

2


Instead of validating by IF whether the file exists or not, you can always remove it after the end of the process if :

// espera o cmd.exe terminar
process.WaitForExit();

string strFile = @"C:\Users\Desktop\ConsoleApplication1\ConsoleApplication1\Source.exe";

if(File.Exists(strFile)
    File.Delete(strFile);

Refactored code:

private void btnExecutar_Click(object sender, EventArgs e)
{
    string strPasta = @"C:\Users\Desktop\ConsoleApplication1\ConsoleApplication1";
    string strFicheiro = $"{strPasta}\\Source.exe";
    string retornar = string.Empty;

    try
    {
        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe")
        {
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        Process process = Process.Start(processStartInfo);

        process.StandardInput.WriteLine($"cd {strPasta}");
        process.StandardInput.WriteLine("g++ -o Source Source.cpp");

        retornar = process.StandardOutput.ReadLine();

        process.StandardInput.WriteLine("exit");
        process.WaitForExit();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (File.Exists(strFicheiro))
            File.Delete(strFicheiro);

        txtRetorno.Text = retornar;
    }
}

Browser other questions tagged

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