C # is equivalent to fork () / exec ()

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

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 for * nix, where I would use fork and then exec in child, then wait for the child to finish work. But I have no idea where to even start looking for something like that.

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

Thank.

+2
source share
3 answers

You can use a class Process. It allows you to specify some parameters about how you want to execute it, and also provides a method that waits for the process to complete before executing the next statement.

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

basically what you can do:

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

 p.WaitForExit();

( , - ):

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

, , .... , , , .

+6
var p = System.Diagnostics.Process.Start("notepad");
p.WaitForExit();
+4

You can use the Process class to start external processes. This will allow you to run arbitrary programs.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

+2
source

Source: https://habr.com/ru/post/1678332/


All Articles