我尝试使用C#GUI连接到OpenVPN。我的代码如下:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "openvpn",
Arguments = "--config config.ovpn",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true
}
};
proc.OutputDataReceived += (s, args) =>
{
debug.WriteLine(args.Data);
};
proc.ErrorDataReceived += (s, args) =>
{
debug.WriteLine("ERROR: " + args.Data);
};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
我将以下输出输入调试窗口:
Tue Apr 26 19:04:58 2016 OpenVPN 2.3.10 x86_64-w64-mingw32 [SSL (OpenSSL)] [LZO] [PKCS11] [IPv6] built on Mar 10 2016
Tue Apr 26 19:04:58 2016 Windows version 6.1 (Windows 7)
Tue Apr 26 19:04:58 2016 library versions: OpenSSL 1.0.1s 1 Mar 2016, LZO 2.09
这意味着它应该工作正常。现在问题是,我没有得到Enter Auth Username:
行(应该在那里),所以我无法解析输出并传递我的凭据(如果我在CMD上运行相同的命令,它有效。)
我在尝试一段时间时尝试了以下操作(因此,一旦输出包含InputStream
,它会自动向library versions:
发送新行:)
[...]
proc.OutputDataReceived += (s, args) =>
{
debug.WriteLine(args.Data);
if(args.Data != null) if(args.Data.Contains("library versions:")) proc.Write("\r\n");
};
[...]
现在我的输出如下:
Tue Apr 26 19:10:11 2016 OpenVPN 2.3.10 x86_64-w64-mingw32 [SSL (OpenSSL)] [LZO] [PKCS11] [IPv6] built on Mar 10 2016
Tue Apr 26 19:10:11 2016 Windows version 6.1 (Windows 7)
Tue Apr 26 19:10:11 2016 library versions: OpenSSL 1.0.1s 1 Mar 2016, LZO 2.09
ERROR: Enter Auth Username:
Tue Apr 26 19:10:11 2016 ERROR: Auth username is empty
Tue Apr 26 19:10:11 2016 Exiting due to fatal error
ERROR:
因此,在我给他换行后,他写了我想要的错误信息流。他为什么表现得那么奇怪?而且,更重要的是,我可以做些什么来获得这条线而不必写新线?