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.
This is one of the most insecure things I’ve ever seen in my life.
– Maniero
Why not make a
File.Exists
right after theprocess.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.– João Martins
It’s just that I’m doing it for academic use, it’s just the code part of my TCC project
– Alex M.
So, if you remove the file after you finish the process, you can?
– João Martins
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.
– Alex M.
Done :)! Happy to have helped!
– João Martins