将 svn 的输出读入字符串

发布于 2024-08-07 21:59:08 字数 872 浏览 2 评论 0原文

好的,所以在我想到通过 SSH 连接到服务器并使用 svn 命令行客户端而不是远程桌面(这并不是什么想法)之后,我和我的老板决定,如果我们能够从单个本地网页(这仅适用于我们的开发服务器)。 现在,我确实让这个工作(一次),但它经常不起作用。

我正在使用以下代码:


        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 应用程序的输出,并将其写入页面,但事实并非如此(除非在实际更新的极少数情况下,但这不是我特别需要输出的时候!)

任何人都可以发现问题?

Ok, so after my idea of SSHing to a server and using the svn command line client instead of remote desktop (not much of an idea tbh), me and my boss have decided it would be rather better if we could update each project from a single local web-page (this is only for our development server).
Now, I did get this to work (once), however it often does not.

I am using the following code:


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

in theory, this should collect the output from the svn app, and write it to the page, however it does not (unless in the rare case when it actually updated, however that is not when I particularly need the output!)

Can anyone spot the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒绊 2024-08-14 21:59:08

下面是从我的一个应用程序中获取的一些代码 - 它基本上只是 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()
}

Here is some code taken from one of my apps - its basically just the MSDN sample. (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()
}
一紙繁鸢 2024-08-14 21:59:08

不完全是您原来问题的答案,但另一种方法可能是使用 SharpSvn (http://sharpsvn.open .collab.net)。通过让您更直接地访问 API,您可能会获得更好的控制和结果。

我用它来监视和更新 svn 工作区,它似乎完成了工作。

Not quite an answer to your original question, but a different approach might be to use SharpSvn (http://sharpsvn.open.collab.net). By giving you more direct access to the API, it might give you better control and results.

I've used it to monitor and update svn work areas and it seemed to get the job done.

痕至 2024-08-14 21:59:08

我认为您需要

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

WaitToExit() 之后

移动您可能还想重定向标准错误,以防发生不好的情况,您可能想了解它。

您还可以通过执行 string text= process.StandardOutput.ReadToEnd(); 来缩短代码

I think you need to move

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

after WaitToExit()

You may also want to redirect standard error in case something bad happens, you probably want to know about it.

Also you can shorten your code by doing string text= process.StandardOutput.ReadToEnd();

记忆里有你的影子 2024-08-14 21:59:08

您需要在进程运行时定期从进程的标准输出管道中读取数据。如果您不这样做,那么标准输出缓冲区将填满,Windows 将暂停该进程并等到其清除后再继续。当然,您的进程处于 WaitForExit() 状态,因此出现了死锁。

这是一个通用答案,因为我对 .NET 进程管理原语不够熟悉,无法给出示例。然而,其原理与任何其他管道输出系统相同。

You will need to periodically read from the standard output pipe of the process while it is running. If you don't do this, then the standard output buffer will fill up and Windows will suspend the process and wait until it's clear before continuing. Of course, your process is sitting in a WaitForExit() so you have a deadlock.

This is a generic answer because I'm not familiar enough with the .NET process management primitives to give an example. However, the principle is the same as in any other piped output system.

请远离我 2024-08-14 21:59:08

首先我会推荐 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)
                );
    }

First I would recomend Rusty's suggestion. Secondly you can review this code for a working example of capturing process output.

If you just want to use the wrapper class from the link here is what you need:

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)
                );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文