0

Okay, so i coded a small app that starts a process and then it opens openvpn. So the first 5-6 times it worked it actually starts the console and connects etc.

Now i only added some more things in the form (didn't touch the process code) now when i click on my connect button all it does is, it opens and immediatly close so basically its not working anymore.

I have been thinking for hours and trying to solve this issue but nothing helps, and everything was working for the first 5-6 times i tested it.

this is my code :

  private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "")
            {
                MessageBox.Show("Please select a server");
            }

            else if (comboBox1.Text == "server")
            {
            button1.Enabled = false;
            string ServerFileContents = richTextBox1.Text;
            File.WriteAllText("server.ovpn", ServerFileContents);
            string args = "--config server.ovpn";
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = Application.StartupPath + @"\bin\openvpn.exe";
            startInfo.Arguments = args;
            startInfo.Verb = "runas";
            process.StartInfo = startInfo;
            process.Start();
            button2.Enabled=true;
            button1.Text = "Connected";
            }   
        }

I have added "requireadministrator" aswell to check if it will work then. I have added some threading sleep to see if it will work but nothing helps.

What am i doing wrong here?

Thanks in advance!

UPDATE : added a console log but its empty so why would it not even open openvpn.exe even it worked for the first 5 times? Also i have removed the requireadministrator to see if the exe is opening and it does open. but i dont know why close instantly.

  • I am guessing your button1_Click event handler will likely exit immediately, leaving the Process object to be collected so quickly that it won't be able to perform its task. Declare the `process` variable on the class level instead, so that it will continue to exist after the event handler exits. – Bent Tranberg Feb 12 '22 at 12:29
  • Thank you for the quick help, i have tried to put it on its own class but it does still the same. It is very confusing at it was working the first like 5 times and now it just immediatly closes leaving the log also empty (so basically doing nothing). I tried also to change the paths but not working (also tried new openvpn.exe). –  Feb 12 '22 at 12:40
  • And you removed the declaration inside the handler? Something else; There's a slight chance there may be dead processes or other resources left dangling, that mess up things. Kill left behind processes with Task Manager if you find them there, or reboot if you want to be absolutely sure you start clean. – Bent Tranberg Feb 12 '22 at 13:00
  • Why are you posting the exact same question twice? – Bent Tranberg Feb 12 '22 at 13:03
  • Deleted must be something wrong thanks. Yes i removed the declaration inside the handler. i rebooted and checked task manager aswell, but unfortunatly no succes. –  Feb 12 '22 at 13:12
  • i am even more confused if i add this line of code from openvpn.exe in another project it works (maybe the same like 5-6 times i dont know but it works). So i am really confused at his point as my current project have the same codes etc –  Feb 12 '22 at 13:47
  • Maybe compare the files of the two projects. – Bent Tranberg Feb 12 '22 at 16:17

0 Answers0