C# - 我无法将我的进程输出转储到文件中
C# - I can't dump to a file the output of my process-ish
我一直在摆弄 C#,在代码的某个时刻,我需要将外部 .exe 的输出转储到 .txt 中。我通过启动 cmd.exe 然后加载程序及其属性以及 >
运算符来实现。但是现在,当我执行程序时,甚至都没有创建文件。同时,如果我在程序中输入传递给 cmd 的完全相同的代码:
"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"
直接进入命令提示符,它确实被转储。我一直在四处寻找,发现了 很多 的信息,但遗憾的是到目前为止没有任何帮助,所以我决定问问自己。
我附上我认为与此相关的代码块。
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...
如果您查看 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)) { }
您正在创建的 Process
class 有这个有用的小东西 属性:
When a Process writes text to its standard stream, that text is normally displayed on the console. By redirecting the StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file.
您需要做的就是确保将 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);
//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);
}
}
}
我一直在摆弄 C#,在代码的某个时刻,我需要将外部 .exe 的输出转储到 .txt 中。我通过启动 cmd.exe 然后加载程序及其属性以及 >
运算符来实现。但是现在,当我执行程序时,甚至都没有创建文件。同时,如果我在程序中输入传递给 cmd 的完全相同的代码:
"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"
直接进入命令提示符,它确实被转储。我一直在四处寻找,发现了 很多 的信息,但遗憾的是到目前为止没有任何帮助,所以我决定问问自己。 我附上我认为与此相关的代码块。
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...
如果您查看 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)) { }
您正在创建的 Process
class 有这个有用的小东西 属性:
When a Process writes text to its standard stream, that text is normally displayed on the console. By redirecting the StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file.
您需要做的就是确保将 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);
//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);
}
}
}