C# - 我无法将进程的输出转储到文件中
本文关键字:转储 输出 文件 进程 | 更新日期: 2023-09-27 18:35:46
我一直在弄乱 C#,在代码的某一刻,我需要将外部.exe的输出转储到.txt中。我通过启动cmd.exe然后加载程序,其属性加上>
操作器来做到这一点。但是现在,当我执行程序时,甚至没有创建文件。同时,如果我输入与程序中传递给cmd完全相同的代码:
"o:''蒸汽''蒸汽应用''共同''反恐精英全球攻势''bin''demoinfogo.exe" "O:''蒸汽''蒸汽应用''共同''反恐精英全球攻势''csgo''testfile.dem" -游戏事件 -无脚步 -死亡SCSV -无预热> "o:''蒸汽''蒸汽应用''共同''反恐精英全球攻势''人口垃圾场.txt"
直接进入命令提示符,它确实被转储。我一直在环顾四周,我找到了很多信息,但遗憾的是到目前为止没有什么对我有足够的帮助,所以我决定问问自己。我附加了我认为与此相关的代码块。
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...
您正在创建的Process
类具有以下有用的小属性:
过程.标准输出
当进程将文本写入其标准流时,该文本通常会显示在控制台上。通过重定向标准输出流,可以操作或抑制进程的输出。例如,您可以筛选文本,以不同的方式设置文本格式,或将输出同时写入控制台和指定的日志文件。
您需要做的就是确保将 StandardOutput 重定向到此流(使用 ProcessStartInfo
中的 RedirectStandardOutput
属性),然后可以从该流读取输出。下面是 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);
如果您查看 CMD 的帮助(通过键入 CMD /?
访问),您将看到以下选项:
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
如果没有这些开关之一,CMD 不会将您提供的字符串解释为要执行的命令。
当我编写如下所示的短程序时,它成功地生成了一个文件......但前提是我使用/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)) { }
//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);
}
}
}