簡體   English   中英

將svn的輸出讀入字符串

[英]Read output from svn into a string

好吧,所以在我想到SSH服務器並使用svn命令行客戶端而不是遠程桌面(不是很多想法tbh)后,我和我的老板已經決定如果我們可以更新每個項目會更好單個本地網頁(僅適用於我們的開發服務器)。 現在,我確實讓它工作(一次),但它通常不會。

我使用以下代碼:


        ProcessStartInfo start = new ProcessStartInfo("C:\Program Files (x86)\CollabNet\Subversion Client\svn.exe", "update " + UpdatePath);
        start.RedirectStandardOutput = true;
        start.UseShellExecute = false;
        start.ErrorDialog = false;
        start.CreateNoWindow = true;
        start.WindowStyle = ProcessWindowStyle.Hidden;
        Process process = Process.Start(start);
        StreamReader output = process.StandardOutput;
        string text = output.ReadToEnd();
        process.WaitForExit();
        Response.Write(text + "<br />" + UpdatePath);

理論上,這應該收集svn應用程序的輸出,並將其寫入頁面,但它不會(除非在極少數情況下它實際更新,但是當我特別需要輸出時!)

誰能發現問題?

不是原始問題的答案,但使用SharpSvn( http://sharpsvn.open.collab.net )的方法可能不同。 通過為您提供更直接的API訪問權限,它可以為您提供更好的控制和結果。

我用它來監視和更新svn工作區域,似乎完成了工作。

以下是我的一個應用程序中的一些代碼 - 它基本上只是MSDN示例。 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx

private void SvnOutputHandler(object sendingProcess,
                                      DataReceivedEventArgs outLine)
{
    Process p = sendingProcess as Process;

    // Save the output lines here
}


private void RunSVNCommand()
{
    ProcessStartInfo psi = new ProcessStartInfo("svn.exe",
                                                string.Format("update \"{0}\" {1}", parm1, parm2));

    psi.UseShellExecute = false;
    psi.CreateNoWindow = true;

    // Redirect the standard output of the sort command.  
    // This stream is read asynchronously using an event handler.
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;

    Process p = new Process();

    // Set our event handler to asynchronously read the sort output.
    p.OutputDataReceived += SvnOutputHandler;
    p.ErrorDataReceived += SvnOutputHandler;
    p.StartInfo = psi;

    p.Start();

    p.BeginOutputReadLine();
    p.BeginErrorReadLine();

    p.WaitForExit()
}

我想你需要搬家

StreamReader output = process.StandardOutput;
string text = output.ReadToEnd();

WaitToExit()

您可能還想重定向標准錯誤,以防出現不良情況,您可能想知道它。

您還可以通過執行string text= process.StandardOutput.ReadToEnd();來縮短代碼string text= process.StandardOutput.ReadToEnd();

在運行過程中,您需要定期讀取進程的標准輸出管道。 如果不這樣做,那么標准輸出緩沖區將填滿,Windows將暫停該過程並等到它清除后再繼續。 當然,您的進程位於WaitForExit()因此您遇到了死鎖。

這是一個通用的答案,因為我不熟悉.NET進程管理原語來舉例。 但是,原理與任何其他管道輸出系統相同。

首先,我會建議Rusty的建議。 其次,您可以查看此代碼以獲取捕獲過程輸出的工作示例。

如果您只想使用鏈接中的包裝類,那么您需要:

using CSharpTest.Net.Processes;
    static void Update(string sourcePath, Action<string> output)
    {
        ProcessRunner run = new ProcessRunner("svn.exe", "update", "{0}");
        run.OutputReceived +=
            delegate(Object o, ProcessOutputEventArgs e) { output(e.Data); };
        int exitCode = run.RunFormatArgs(sourcePath);
        if (exitCode != 0)
            throw new ApplicationException(
                String.Format("SVN.exe returned {0}.", exitCode)
                );
    }

暫無
暫無

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

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