Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to Process.Start() without separate arguments

I'm writing a simple application that's required to run arbitrary commands, for example:

powershell -File myscript.ps1
cmd /C "ping localhost"

Process.Start() would be perfect except it requires the arguments be given as a separate parameter. Initially I thought I could just split the string on the first space character, but then what if the executable path is quoted and contains spaces? Is there something like Process.Start() which allows you to just give it a string, with or without arguments, and just have it execute it as if it was pasted to a command prompt?

like image 714
TomWardrop Avatar asked Mar 26 '14 00:03

TomWardrop


People also ask

What does Process start do?

Start(ProcessStartInfo) Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

How do I start a Process in VB net?

Start another application using your . NET code As a . NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.

Does Process start async?

Indeed, Process. Start is async and the OP appears to want a synchronous version.


2 Answers

Why don't you just run everything through cmd /C?

Process.Start("cmd", "/C " + command);
like image 194
itsme86 Avatar answered Sep 18 '22 20:09

itsme86


Just call the CreateProcess function directly:

public static class Native
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    private struct STARTUPINFO
    {
        public Int32 cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public Int32 dwX;
        public Int32 dwY;
        public Int32 dwXSize;
        public Int32 dwYSize;
        public Int32 dwXCountChars;
        public Int32 dwYCountChars;
        public Int32 dwFillAttribute;
        public Int32 dwFlags;
        public Int16 wShowWindow;
        public Int16 cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public int dwProcessId;
        public int dwThreadId;
    }

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern bool CreateProcess(
        string lpApplicationName,
        string lpCommandLine,
        IntPtr lpProcessAttributes,
        IntPtr lpThreadAttributes,
        bool bInheritHandles,
        uint dwCreationFlags,
        IntPtr lpEnvironment,
        string lpCurrentDirectory,
        [In] ref STARTUPINFO lpStartupInfo,
        out PROCESS_INFORMATION lpProcessInformation);

    public static void CreateProcessFromCommandLine(string commandLine)
    {
        var si = new STARTUPINFO();
        var pi = new PROCESS_INFORMATION();

        CreateProcess(
            null,
            commandLine,
            IntPtr.Zero,
            IntPtr.Zero,
            false,
            0,
            IntPtr.Zero,
            null,
            ref si,
            out pi);
    }
}

internal class Program
{
    public static void Main()
    {
        Native.CreateProcessFromCommandLine("ping google.com -t");

        Console.Read();
    }
}
like image 32
ken2k Avatar answered Sep 21 '22 20:09

ken2k