简体   繁体   English

C# - 我无法将我的process-ish的输出转储到文件中

[英]C# - I can't dump to a file the output of my process-ish

I've been messing around with C# and in one moment of the code, I need to dump the output of an external .exe into a .txt. 我一直在搞乱C#,在代码的一瞬间,我需要将外部.exe的输出转储到.txt中。 I do it by starting cmd.exe and then loading the program, with its attributes plus the > opperator. 我这样做是通过启动cmd.exe然后加载程序,其属性加上> opperator。 But now, when I execute the program, the file isn't even created. 但是现在,当我执行程序时,甚至都没有创建文件。 Meanwhile, if I input the EXACT same code that is passed to cmd in the program: 同时,如果我在程序中输入传递给cmd的EXACT相同代码:

"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 offensive\\demodump.txt" “o:\\ steam \\ steamapps \\ common \\ counter-strike全球攻势\\ 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 offensive \\ demodump.txt”

directly into the Command Prompt, it does get dumped. 直接进入命令提示符,它确实被转储。 I've been looking around, and I found A LOT of info, but sadlly nothing has helped me enough so far, so I decided to ask myself. 我一直在四处寻找,我发现了很多信息,但到目前为止,没有任何帮助我,所以我决定问自己。 I attach the chunks 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...

If you look at the help for CMD (access by typing CMD /? ) you'll see the following options: 如果您查看CMD的帮助(通过键入CMD /?访问),您将看到以下选项:

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

Without one of those switches, CMD won't interpret the string you provide it as a command to execute. 如果没有其中一个开关,CMD将不会将您提供的字符串解释为执行命令。

When I write a short program like the following, it successfully generates a file... but only if I use either the /C or /K options: 当我编写如下的短程序时,它会成功生成一个文件...... 但只有当我使用/C/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)) { }

The Process class that you're creating has this useful little property: 您正在创建的Process类具有此有用的小属性:

Process.StandardOutput Process.StandardOutput

When a Process writes text to its standard stream, that text is normally displayed on the console. 当Process将文本写入其标准流时,该文本通常显示在控制台上。 By redirecting the StandardOutput stream, you can manipulate or suppress the output of a process. 通过重定向StandardOutput流,您可以操纵或抑制进程的输出。 For example, you can filter the text, format it differently, or write the output to both the console and a designated log file. 例如,您可以过滤文本,以不同方式对其进行格式化,或将输出写入控制台和指定的日志文件。

All you need to do is ensure you're redirecting the StandardOutput to this stream (using the RedirectStandardOutput property in the ProcessStartInfo ) and then you can read the output from that stream. 您需要做的就是确保将StandardOutput重定向到此流(使用ProcessStartInfoRedirectStandardOutput属性),然后您可以读取该流的输出。 Here's the MSDN sample code, slightly abridged: 这是MSDN示例代码,略有删节:

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's first line:

StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
//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);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM