簡體   English   中英

c# 使用 Process.Start(ProcessStartInfo info) 的程序不起作用

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


我嘗試使用一個小的 c# 程序打開一個 OpenVPN 連接。
下面是我使用的代碼。\\

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


這段代碼確實有效,但我只在計算機上編譯了它。
在我們的服務器(Win server 2019)上,我收到錯誤消息:

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



我不明白錯誤消息的最后一行來自哪里。

你能幫我解決這個問題嗎?

我假設您使用的是德國窗戶或類似的窗戶。

注意windows資源管理器中文件路徑的顯示值並不反映真實路徑,見圖。

C:\Programme -> C:\Program Files

您可以通過單擊地址欄中的 來顯示真實路徑。

顯示路徑與真實路徑

嘗試:

static void Main(string[] args)
    {
        Process rs = new Process();
        var netCredential = new System.Net.NetworkCredential("User", "PWD", "Domain");
        System.Environment.CurrentDirectory = ".\\";
        var vpnPath = Path.Combine(
           Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
           @"openvpn\bin\openvpn.exe");
        ProcessStartInfo info = new ProcessStartInfo
        {
            FileName = vpnPath,
            Arguments = "--config \"C:\\Program Files\\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(vpnPath)
        };
        var p = Process.Start(info);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM