In my Winform app, I execute a exe file using cmd Process. The exe file needs to input username and passowrd on line "Enter Username" & "Enter Password". I am not able to input the username and password, somehow the process gets exited only and doesn't work out only. I added checking of ThreadState and WaitReason also, but there are no threads in process.Threads. I also need the output. If I type output before input process, then it doesn't reach till input and if I put input before output, then also the input is not accepeted or so. Just receive the output same all time. Here is the code :

```
        public bool StartOpenVPN()
    {
        bool installed = false;
        ProcessStartInfo processInfo = null;
        Process process = null;
        try
        {
            string command = "files\\openvpn --config files\\client.ovpn";
            Console.WriteLine("Command = " + command);

            processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardInput = true;
            processInfo.RedirectStandardOutput = true;
            Console.WriteLine("Opening cmd");

            process = new Process();
            process.StartInfo = processInfo;
            process.Start();

            StreamWriter sw = process.StandardInput;
            sw.WriteLine("foo");
            sw.WriteLine("*baa");
            sw.Flush();
            sw.Close();

            process.BeginOutputReadLine();
            process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);

            Console.WriteLine("Finished cmd");
        }
        catch (Exception e)
        {
            Console.WriteLine("Errror Installing Tap Adapter : " + e.Message);
            Console.WriteLine(e.StackTrace);
        }
        finally
        {
            processInfo = null;
            if (process != null)
            {
                process.Close();
                process = null;
            }
        }
        return installed;
    }

    private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        string d = e.Data;

        if (!string.IsNullOrEmpty(d))
        {
            Console.WriteLine("Line = " + d);
        }
    }

```

The only output I get is :

```
Line = Wed Mar 09 12:33:00 2011 OpenVPN 2.1.1 i686-pc-mingw32 [SSL] [LZO2] [PKCS11] built on Feb 17 2010
Line = Wed Mar 09 12:33:00 2011 ERROR: could not read Auth username from stdin
Line = Wed Mar 09 12:33:00 2011 Exiting

```

Why it doesn't accept the input or even show the line "Enter Username" in output ? I can't makeout where am I going wrong, but it seems am going wrong somewhere. Kindly help me with this issue am stuck badly and have tried many times and things and have spent lots of time after this. 

Any help is highly appreciated.

Thanks

@Fun Mun : here is the updated code. It just goes to process\_Exited on getting any stream input/output :

```
        private void initProcess()
    {
        processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardInput = true;
        processInfo.RedirectStandardOutput = true;
        processInfo.WindowStyle = ProcessWindowStyle.Normal;

        process = new Process();
        process.StartInfo = processInfo;
        process.Exited += new EventHandler(process_Exited);
        return;
    }

    void process_Exited(object sender, EventArgs e)
    {
        Console.WriteLine("Into process_Exited....");
        processInfo = null;
        if (process != null)
        {
            sw = null;
            process.Close();
            process = null;
        }
    }

    public bool StartOpenVPN()
    {
        bool installed = false;
        try
        {
            Console.WriteLine("Command = " + command);

            Console.WriteLine("Opening cmd");

            initProcess();
            process.Start();

            sw = process.StandardInput;
            Console.WriteLine("Has Exited after SW = " + process.HasExited.ToString()); // RETURS FALSE BUT GOES TO process_Exited & for next line results is NullPointerException
            sw.WriteLine("foo");
            sw.WriteLine("*baa");
             //sw.Flush();
            //sw.Close();

            //process.BeginOutputReadLine();
            //process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
             while (process.HasExited == false)
             {
                 string d = process.StandardOutput.ReadLine();
                 Console.WriteLine("Line = " + d);
             }

            process.WaitForExit();
            Console.WriteLine("Finished cmd");
        }
        catch (Exception e)
        {
            Console.WriteLine("Errror Opening : " + e.Message);
            Console.WriteLine(e.StackTrace);
        }

        return installed;
    }

```

Untill process.BeginOutputReadLine(); & output handler was not removed, it kept waiting (may be for entry)like hanged out. The moment removed those 2 output lines and added while loop, process just gets exit on any stream call i.e. process.StandardOutput; OR StandardInput whichever comes the next and throws NullPointerException.
Have I made any mistake in the code as instructed by you ?

---

@Fun Mum : Even after using and updating your edited version of the code, I end up the same. I updated your code, modified it a bit seeing that after 1st line output the openvpn asks for username and then passowrd, then their is no input. I get proper console outputs but the results not as expected. Here is the code :

```
        public bool StartOpenVPN()
    {
        bool installed = false;
        int lineNo = 0;
        try
        {
            Console.WriteLine("Command = " + command + "\nOpening cmd");

            initProcess();
            process.Start();

            sw = process.StandardInput;
            Console.WriteLine("Has Exited after SW = " + process.HasExited.ToString());
            //sw.WriteLine("123b5df33f");
            //sw.WriteLine("*3FgYxyt");
            //Console.WriteLine("Has Exited after writing data = " + process.HasExited.ToString());

             while (process.HasExited == false)
             {
                 string d = process.StandardOutput.ReadLine();
                 Console.WriteLine("Line = " + d);
                 lineNo++;
                 if (lineNo == 1)
                 {
                     Console.WriteLine("Writing Details");
                     sw.WriteLine("foo");
                     sw.Flush();
                     Console.WriteLine("Wrote Username");
                     sw.WriteLine("*baa");
                     sw.Flush();
                     Console.WriteLine("Wrote Password");
                 }
             }

             process.Close();
             process = null;
            Console.WriteLine("Finished cmd");
        }
        catch (Exception e)
        {
            Console.WriteLine("Errror Installing Tap Adapter : " + e.Message);
            Console.WriteLine(e.StackTrace);
        }

        return installed;
    }

```

The output is :

```
Command = files\openvpn --config files\client.ovpn

```

Opening cmd
Has Exited after SW = False
Line = Fri Mar 11 18:10:06 2011 OpenVPN 2.1.1 i686-pc-mingw32 [SSL] [LZO2] [PKCS11] built on Feb 17 2010
Writing Details // AFTER 1ST LINE IT IS TRYNG TO WRITE
Wrote Username
Wrote Password
Line = Fri Mar 11 18:10:06 2011 ERROR: could not read Auth username from stdin
Line = Fri Mar 11 18:10:06 2011 Exiting
Line = 
Finished cmd

Couldn't make out if application is not able to throw input to stdin properly or openvpn is not able to accept it. I also tried sw.WriteLine("foo" + ConsoleKey.Enter);, that also produced same results. Was wondering & trying to see the cmd window on while executing/debugging to know the exact results, but couldn't see that also.

If we run openvpn via command prompt normally, we get is :

```
D:\>files\\openvpn --config files\\client.ovpn

```

Fri Mar 11 18:03:48 2011 OpenVPN 2.1.1 i686-pc-mingw32 [SSL] [LZO2] [PKCS11] bui
lt on Feb 17 2010
Enter Auth Username:

Puzzled out with this, really.