簡體   English   中英

使用System.Diagnostics.Process在命令行上的文件中管道

[英]Piping in a file on the command-line using System.Diagnostics.Process

我正在嘗試運行命令行程序並在文件中管道。 該命令在命令行上運行正常,但我似乎無法使用C#中的Process對象。 這是我發出的命令:

“C:\\ Servers \\ CollabNet Subversion Server \\ svnadmin”加載C:\\ Repositories \\ TestLoad <C:\\ Temp \\ test.dump

除了上面的命令之外,此函數適用於我傳遞給它的所有其他命令:

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;
}

我已經嘗試過這個功能的幾種不同組合,但我無法讓它工作。 我一直收到“系統找不到指定文件”的消息。 我已經在這里待了大約一個星期,我想我需要一些眼睛來看看我做錯了什么。

馬克和盧克都給了我正確的方向。 我無法使用任何一個答案,因為我必須這樣做才能在Linux中運行Mono。 所以我最終按照建議寫入了StandardInput。 這是有效的代碼:

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;
}

我不是百分百肯定,但我的猜測是你必須使用其StandardInput屬性手動將數據從test.dump推送到進程。

否則,您可以嘗試使用cmd.exe來執行命令:

var psi = new ProcessStartInfo( "cmd.exe /c \"" + command "\"" );

原因是“>”,“<”和“|” 運算符由shell本身實現(cmd.exe)。

我的猜測是你應該掛鈎StandardInput並自己寫入文件。 您可以嘗試使用ShellExecute = true,但我不確定它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM