Martin
Martin

Reputation: 61

c# Programm using Process.Start(ProcessStartInfo info) does not work

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.

Upvotes: 0

Views: 264

Answers (1)

LewxX
LewxX

Reputation: 38

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);
    }

Upvotes: 2

Related Questions