我一直在搞乱C#,在代码的一瞬间,我需要将外部.exe的输出转储到.txt中。我是通过启动cmd.exe然后加载程序及其属性加上>
操作符来实现的。但是现在,当我执行程序时,文件甚至都没有创建。同时,如果我在程序中输入传递给cmd的EXACT相同代码:
" o:\ steam \ steamapps \ common \反击全球攻势\ bin \ demoinfogo.exe" " O:\ Steam \ SteamApps \ common \ Counter-Strike Global Offensive \ csgo \ testfile.dem" -gameevents -nofootsteps -deathscsv -nowarmup> " o:\ steam \ steamapps \ common \反击全球攻势\ decodemp.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...
答案 0 :(得分:3)
如果您查看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)) { }
答案 1 :(得分:3)
您正在创建的Process
类具有此有用的小属性:
当Process将文本写入其标准流时,该文本通常显示在控制台上。通过重定向StandardOutput流,您可以操纵或抑制进程的输出。例如,您可以过滤文本,以不同方式对其进行格式化,或将输出写入控制台和指定的日志文件。
您需要做的就是确保将StandardOutput重定向到此流(使用RedirectStandardOutput
中的ProcessStartInfo
属性),然后您可以读取该流的输出。这是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);
答案 2 :(得分:0)
//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);
}
}
}