C#相当于叉()/ EXEC() [英] C# equivalent to fork()/exec()

查看:142
本文介绍了C#相当于叉()/ EXEC()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发需要调用外部程序,但需要等待它执行程序。这是在C#中正在做(而我是全新的,但是有很多的经验的C ++,Qt的,和C)和CreateProcess的似乎并没有成为什么我要找(启动的过程中,会忘掉它,这我不需要)。

I'm developing a program that needs to call an outside program, but needs to wait for it to execute. This is being done in C# (to which I am brand new, but have lots of experience in C++, Qt, and C) and CreateProcess does not seem to be what I'm looking for (starts the process, then forgets it, which I don't need).

这是我的第一个Windows项目之一(或至少,只有Windows和肯定只有.NET),我做这样的事情的* nix中,我会用叉子,然后exec的在更适用孩子,然后等待孩子结束。但我不知道在哪里,甚至开始希望做这样的事情。

This is one of my first Windows projects (or at least, only Windows and definitely only .NET) and I'm much more used to doing this sort of thing for *nix where I would use fork and then exec in the child, then wait for the child to terminate. But I have no idea where to even start looking to do something like this.

呵呵,我就是pretty的肯定,我被困在.NET,因为我需要读取访问注册表来完成这个项目和.NET的注册表访问绝对是惊人的(在我看来,我不有什么比较它)。

Oh, and I'm pretty sure I'm stuck in .NET because I need read access to the registry to complete this project and .NET's registry access is absolutely amazing (in my opinion, I don't have anything to compare it to).

感谢。

推荐答案

您可以使用进程类。它可以让你指定有关要如何执行它,并且还提供了等待的过程中,退出执行下一个语句前一种方法的一些选项。

You can use the Process class. It lets you specify some options about how you want to execute it, and also provides a method which waits the process to exit before executing the next statement.

看看这个链接(在MSDN参考): http://msdn.microsoft.com/fr-fr/library /system.diagnostics.process.aspx

look at this link (the msdn reference): http://msdn.microsoft.com/fr-fr/library/system.diagnostics.process.aspx

基本上你可以做的是:

 Process p;
 // some code to initialize it, like p = startProcessWithoutOutput(path, args, true);

 p.WaitForExit();

在初始化过程中(这只是一些code我用了一次的地方)的一个例子:

an example of initializing the process (that's just some code I used once somewhere):

    private Process startProcessWithOutput(string command, string args, bool showWindow)
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(command, args);
        p.StartInfo.RedirectStandardOutput = false;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = !showWindow;
        p.ErrorDataReceived += (s, a) => addLogLine(a.Data);
        p.Start();
        p.BeginErrorReadLine();

        return p;
    }

,你可以在这个$ C $看到c您还可以做一些输出重定向,错误重定向....如果你在课堂挖我想你会发现很快你所需要的。

as you can see in this code you can also do some output redirection, error redirection.... If you dig in the class I think you'll find quite quickly what you need.

这篇关于C#相当于叉()/ EXEC()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆