C # - I cannot flush the output of my process-ish to a file

I was working with C # and at one point of code I need to dump the output of an external .exe to a .txt. I do this by running cmd.exe and then loading the program with its attributes plus opperator >

. But now, when I run the program, the file is not even created. Meanwhile, if I enter the EXACT same code that is passed to cmd in the program:

"o: \ steam \ steamapps \ common \ counter-strike global offensive \ bin \ demoinfogo.exe" "O: \ Steam \ SteamApps \ common \ Counter-Strike Global Offensive \ csgo \ testfile.dem" -gameevents - nofootsteps -deathscsv -nowarmup> "o: \ steam \ steamapps \ common \ counter-strike global onensive \ demodump.txt"

directly on the command line, it does . I looked around and I found A LOT of information, but unfortunately nothing has helped me so far, so I decided to ask myself. I am attaching pieces of code that I think are relevant to this.

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = "CMD.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;

if (checkBox1.Checked)
{
    arguments += " -gameevents";
    if (checkBox2.Checked)
    {
        arguments += " -nofootsteps";
    }
    if (checkBox3.Checked)
    {
        arguments += " -extrainfo";
    }
}
if (checkBox4.Checked)
{
    arguments += " -deathscsv";
    if (checkBox5.Checked)
    {
        arguments += " -nowarmup";
    }
}

if (checkBox6.Checked)
{
    arguments += " -stringtables";
}
if (checkBox7.Checked)
{
    arguments += " -datatables";
}
if (checkBox8.Checked)
{
    arguments += " -packetentites";
}
if (checkBox9.Checked)
{
    arguments += " -netmessages";
}
if (dumpfilepath == string.Empty)
{
    dumpfilepath =  getCSGOInstallationPath() + @"\demodump.txt";
}

baseOptions = @"""" + demoinfogopath + @"""" + " " + @"""" + demofilepath + @"""" + arguments;
startInfo.Arguments = baseOptions + " > " + @"""" + dumpfilepath + @"""";

try  
{
    using (exeProcess = Process.Start(startInfo))
         ....a bunch of code...

      

+3


source to share


3 answers


The class Process

you are creating has this useful little property:

Process.StandardOutput

When a process writes text to standard stream, that text is usually displayed on the console. By redirecting the StandardOutput stream, you can manipulate or suppress the output of the process. For example, you can filter text, format it differently, or write output to both the console and a specified log file.



All you have to do is make sure you redirect StandardOutput to this stream (using property RedirectStandardOutput

in ProcessStartInfo

) and then you can read the output from that stream. Here's a sample MSDN code, slightly abbreviated:

Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn");
myProcessStartInfo.UseShellExecute = false; // important!
myProcessStartInfo.RedirectStandardOutput = true; // also important!
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

// Here we're reading the process output first line:

StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);

      

+3


source


If you look at the CMD help (access by typing CMD /?

), you will see the following options:

/C   Carries out the command specified by string and then terminates 
/K   Carries out the command specified by string but remains

      

Without one of these switches, CMD will not interpret the line you provide as the command to execute.



When I write a short program like the following, it successfully creates the file ... but only if I use the options /C

or /K

:

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = "CMD.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;

var command = @"echo test > c:\users\myusername\Desktop\test.txt";
var args = "/C " + command;
startInfo.Arguments = args;

using (var process = Process.Start(startInfo)) { }

      

+3


source


//Hi you could try this to build your process like this.
public class Launcher
{
    public Process CurrentProcess;
    public string result = null;

    public Process Start()
    {
        CurrentProcess = new Process
        {
            StartInfo =
            {
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                WorkingDirectory = @"C:\",
                FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe")
            }
        };
        CurrentProcess.Start();

        return CurrentProcess;
    }

    //Start the process to get the output you want to add to your .txt file:
    private void writeOuput()
    {
        Currentprocess = new process();
        Start()

        CurrentProcess.StandardInput.WriteLine("Your CMD");
        CurrentProcess.StandardInput.Close();

        result = CurrentProcess.StandardOutput.ReadLine();
        CurrentProcess.StandardOutput.Close()

        //Then to put the result in a .txt file:
        System.IO.File.WriteAllText (@"C:\path.txt", result);
    }
}

      

}

0


source







All Articles