コード例 #1
0
ファイル: Process.cs プロジェクト: ForNeVeR/pnet
        // Start a process.
        public bool Start()
        {
            ProcessStartInfo.ProcessStartFlags flags;

            // Validate the start information.
            if (startInfo == null || startInfo.FileName == String.Empty)
            {
                throw new InvalidOperationException
                          (S._("Invalid_ProcessStartInfo"));
            }
            flags = startInfo.flags;
            if ((flags & ProcessStartInfo.ProcessStartFlags.UseShellExecute)
                != 0)
            {
                if ((flags & (ProcessStartInfo.ProcessStartFlags
                              .RedirectStdin |
                              ProcessStartInfo.ProcessStartFlags
                              .RedirectStdout |
                              ProcessStartInfo.ProcessStartFlags
                              .RedirectStderr)) != 0)
                {
                    // Cannot redirect if using shell execution.
                    throw new InvalidOperationException
                              (S._("Invalid_ProcessStartInfo"));
                }
            }

            // Close the current process information, if any.
            Close();

            // If attempting to start using the current process,
            // then we want to do "execute over the top" instead,
            // replacing the current process with a new one.
            if (IsCurrentProcess(this))
            {
                flags |= ProcessStartInfo.ProcessStartFlags.ExecOverTop;
            }

            // Get the environment to use in the new process if it
            // was potentially modified by the programmer.
            String[] env = null;
            if (startInfo.envVars != null)
            {
                StringCollection      coll = new StringCollection();
                IDictionaryEnumerator e    =
                    (IDictionaryEnumerator)
                    (startInfo.envVars.GetEnumerator());
                while (e.MoveNext())
                {
                    coll.Add(((String)(e.Key)).ToUpper(CultureInfo.InvariantCulture) +
                             "=" + ((String)(e.Value)));
                }
                env = new String [coll.Count];
                coll.CopyTo(env, 0);
            }

            // Get the pathname of the program to be executed.
            String program;

            if (startInfo.UseShellExecute && startInfo.WorkingDirectory != String.Empty && !Path.IsPathRooted(startInfo.FileName))
            {
                program = Path.Combine(startInfo.WorkingDirectory, startInfo.FileName);
            }
            else
            {
                program = startInfo.FileName;
            }

            // Parse the arguments into a local argv array.
            String[] args = ProcessStartInfo.ArgumentsToArgV
                                (startInfo.Arguments);
            argv    = new String [args.Length + 1];
            argv[0] = program;
            Array.Copy(args, 0, argv, 1, args.Length);

            // Start the process.
            IntPtr stdinHandle;
            IntPtr stdoutHandle;
            IntPtr stderrHandle;

            if (!StartProcess(program, startInfo.Arguments, startInfo.WorkingDirectory, argv,
                              (int)flags, (int)(startInfo.WindowStyle),
                              env, startInfo.Verb,
                              startInfo.ErrorDialogParentHandle,
                              out processHandle, out processID,
                              out stdinHandle, out stdoutHandle,
                              out stderrHandle))
            {
                // Checking errno for error
                Errno errno = Process.GetErrno();
                if (errno != Errno.Success)
                {
                    throw new Win32Exception(Process.GetErrnoMessage(errno));
                }
            }

            // Wrap up the redirected I/O streams.
            if (stdinHandle != SocketMethods.GetInvalidHandle())
            {
                stdin = new StreamWriter
                            (new FileStream(stdinHandle, FileAccess.Write, true));
                stdin.AutoFlush = true;
            }
            if (stdoutHandle != SocketMethods.GetInvalidHandle())
            {
                stdout = new StreamReader
                             (new FileStream(stdoutHandle, FileAccess.Read, true));
            }
            if (stderrHandle != SocketMethods.GetInvalidHandle())
            {
                stderr = new StreamReader
                             (new FileStream(stderrHandle, FileAccess.Read, true));
            }

            // Add the process to the list of active children.
            lock (typeof(Process))
            {
                if (children == null)
                {
                    children = new ArrayList();
                }
                children.Add(this);
            }
            return(true);
        }