0

I tried to open a OpenVPN connection using a small c# program.
Below is the code I used.

static void Main(string[] args)
{
    Process rs = new Process();
    var netCredential = new System.Net.NetworkCredential("User", "PWD", "Domain");
    System.Environment.CurrentDirectory = ".\\";
    ProcessStartInfo info = new ProcessStartInfo
    {
        FileName = "c:\\programme\\openvpn\\bin\\openvpn.exe",
        Arguments = "--config c:\\programme\\openvpn\\config\\NAS-Name.ovpn",
        UserName = netCredential.UserName,
        Domain = netCredential.Domain,
        Password = netCredential.SecurePassword,
        UseShellExecute = false,
        //RedirectStandardError = true,
        //RedirectStandardOutput = true,
        //CreateNoWindow = true,
        WorkingDirectory = Path.GetDirectoryName("c:\\programme\\openvpn\\bin\\openvpn.exe")
    };
    var p = Process.Start(info);
}

This code does work, but only on the computer were I compiled it.
On our server (Win server 2019) I get the error:

Unhandled Exception: System.ComponentModel.Win32Exception:System cant find file at System.Diagnostics.ProcessWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at ConnectOpenVPN.Program.Main(String[] args) in C:\User\path\to\Program.cs:Zeile 43

I don´t understand where the last line of the error message comes from.

4
  • 1
    It is probably located in "c:\\program files", not "c:\\programme" Commented Nov 6, 2020 at 8:33
  • The error message seems quite clear though - it can't see the file. adjust the path so it can find the executable on the server Commented Nov 6, 2020 at 8:33
  • 2
    I suggest you use Environment.SpecialFolder Enum like Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)). And then Path.Combine to add the openvpn part. Commented Nov 6, 2020 at 8:59
  • 1
    ^^ I'd also check the existence of .exe and config file before using them. ( if (File.Exists(pathToFile)) ... ) Commented Nov 6, 2020 at 9:05

1 Answer 1

2

I assume you are using a german windows or similar.

Beware that the display value of file pathes in windows explorer does not reflect the real path, see picture.

C:\Programme -> C:\Program Files

You can reveal the real path by clicking in the address bar.

displayed path vs real path

try:

static void Main(string[] args)
    {
        Process rs = new Process();
        var netCredential = new System.Net.NetworkCredential("User", "PWD", "Domain");
        Environment.CurrentDirectory = ".\\";
        var vpnConfigPath = Path.Combine(
           Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
           "openvpn", "config", "NAS-Name.ovpn");
        var vpnPath = Path.Combine(
           Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
           @"openvpn","bin","openvpn.exe");
        ProcessStartInfo info = new ProcessStartInfo
        {
            FileName = vpnPath,
            Arguments = $"--config \"{vpnConfigPath}\"",
            UserName = netCredential.UserName,
            Domain = netCredential.Domain,
            Password = netCredential.SecurePassword,
            UseShellExecute = false,
            //RedirectStandardError = true,
            //RedirectStandardOutput = true,
            //CreateNoWindow = true,
            WorkingDirectory = Path.GetDirectoryName(vpnPath)
        };
        var p = Process.Start(info);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Have a look into Environment.SpecialFolder enum and Environment.GetFolderPath. You'll quickly see why it's a good idea to use them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.