Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4267270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:52:35+00:00 2026-05-21T06:52:35+00:00

In my Winform app, I execute a exe file using cmd Process. The exe

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:52:36+00:00Added an answer on May 21, 2026 at 6:52 am

    This query has been unresolvable for me as openvpn didn’t support accepting stdin from the application.

    Thanks for everybody help and ideas and suggestions.

    Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If we develop a winform app using an MVC architecture, it should not be
I have a web page that is being displaying in a winform app using
Our Winform app consists of a Main.exe and a Launcher.exe. The purpose of the
I have a WinForm app. I compile it, double-click on it. Predictably, csc.exe kicks
My WinForm apps needs to execute complex query with significant execution time, I have
I have a winform app that calls a web service to check for updates.
I built a Winform app several months ago that schedules appointments for repair techs.
In my winform app, I am trying to add a userSetting, although the error
I'm working on a winform app and am interating through a checkboxlist to see
I am building a c# winform app and the framework I inherited uses UI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.