[英]Piping in a file on the command-line using System.Diagnostics.Process
I'm trying to run a command-line program and piping in a file. 我正在尝试运行命令行程序并在文件中管道。 The command works fine on the command-line, but I can't seem to get it to work with the Process object in C#.
该命令在命令行上运行正常,但我似乎无法使用C#中的Process对象。 Here's the command I'm issuing:
这是我发出的命令:
"C:\\Servers\\CollabNet Subversion Server\\svnadmin" load C:\\Repositories\\TestLoad < C:\\Temp\\test.dump “C:\\ Servers \\ CollabNet Subversion Server \\ svnadmin”加载C:\\ Repositories \\ TestLoad <C:\\ Temp \\ test.dump
This function works fine with all the other commands that I pass to it, except for the command above: 除了上面的命令之外,此函数适用于我传递给它的所有其他命令:
public static bool ExecuteSvnCommand( string command, string arguments, out string result, out string errors )
{
bool retval = false;
string output = string.Empty;
string errorLines = string.Empty;
Process svnCommand = null;
var psi = new ProcessStartInfo( command );
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
try
{
Process.Start( psi );
psi.Arguments = arguments;
svnCommand = Process.Start( psi );
StreamReader myOutput = svnCommand.StandardOutput;
StreamReader myErrors = svnCommand.StandardError;
svnCommand.WaitForExit();
if ( svnCommand.HasExited )
{
output = myOutput.ReadToEnd();
errorLines = myErrors.ReadToEnd();
}
// Check for errors
if ( errorLines.Trim().Length == 0 )
{
retval = true;
}
}
catch ( Exception ex )
{
string msg = ex.Message;
errorLines += Environment.NewLine + msg;
}
finally
{
if (svnCommand != null)
{
svnCommand.Close();
}
}
result = output;
errors = errorLines;
return retval;
}
I've tried several different combinations of this function, but I can't get this to work. 我已经尝试过这个功能的几种不同组合,但我无法让它工作。 I keep getting a "The system cannot find the file specified" message.
我一直收到“系统找不到指定文件”的消息。 I've been at this for about a week now, and I think I need a few set of eyes to see what I'm doing wrong.
我已经在这里待了大约一个星期,我想我需要一些眼睛来看看我做错了什么。
Both Mark and Luke gave me the right direction to go. 马克和卢克都给了我正确的方向。 I couldn't use either answer because I had to do this so that it could run with Mono in Linux.
我无法使用任何一个答案,因为我必须这样做才能在Linux中运行Mono。 So I ended up writing to the StandardInput as suggested.
所以我最终按照建议写入了StandardInput。 Here is the code that works:
这是有效的代码:
public static bool ExecuteSvnCommandWithFileInput( string command, string arguments, string filePath, out string result, out string errors )
{
bool retval = false;
string output = string.Empty;
string errorLines = string.Empty;
Process svnCommand = null;
var psi = new ProcessStartInfo( command );
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
try
{
Process.Start( psi );
psi.Arguments = arguments;
svnCommand = Process.Start( psi );
var file = new FileInfo(filePath);
StreamReader reader = file.OpenText();
string fileContents = reader.ReadToEnd();
reader.Close();
StreamWriter myWriter = svnCommand.StandardInput;
StreamReader myOutput = svnCommand.StandardOutput;
StreamReader myErrors = svnCommand.StandardError;
myWriter.AutoFlush = true;
myWriter.Write(fileContents);
myWriter.Close();
output = myOutput.ReadToEnd();
errorLines = myErrors.ReadToEnd();
// Check for errors
if ( errorLines.Trim().Length == 0 )
{
retval = true;
}
}
catch ( Exception ex )
{
string msg = ex.Message;
errorLines += Environment.NewLine + msg;
}
finally
{
if (svnCommand != null)
{
svnCommand.Close();
}
}
result = output;
errors = errorLines;
return retval;
}
I'm not 100% sure but my guess would be that you have to manually push the data from test.dump
to the process using its StandardInput
property. 我不是百分百肯定,但我的猜测是你必须使用其
StandardInput
属性手动将数据从test.dump
推送到进程。
Otherwise you could try using cmd.exe
to execute the command: 否则,您可以尝试使用
cmd.exe
来执行命令:
var psi = new ProcessStartInfo( "cmd.exe /c \"" + command "\"" );
The reason for this is that the ">", "<" and "|" 原因是“>”,“<”和“|” operators are implemented by the shell itself (cmd.exe).
运算符由shell本身实现(cmd.exe)。
My guess is that you should hook the StandardInput and write in the file yourself. 我的猜测是你应该挂钩StandardInput并自己写入文件。 You could try with ShellExecute = true, but I'm not sure it'd work.
您可以尝试使用ShellExecute = true,但我不确定它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.