我试图使用一个小型的OpenVPN程序打开一个c#连接。
下面是我使用的代码。
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)上,我得到了错误:
未处理的异常: System.Diagnostics.ProcessWithCreateProcess(ProcessStartInfo startInfo)在System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start() at ConnectOpenVPN.Program.Main(String[] args)在C:\User\path\to\Program.cs:Zeile 43中System.ComponentModel.Win32Exception:System无法找到文件
我不明白错误信息的最后一行是从哪里来的。
发布于 2020-11-06 09:00:24
我猜你是在用德国窗户或类似的东西。
请注意,windows资源管理器中文件路径的显示值不能反映真实路径,请参阅图片。
C:\Programme -> C:\Program Files您可以通过单击地址栏来显示真正的路径。

尝试:
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);
}https://stackoverflow.com/questions/64711161
复制相似问题