简体   繁体   English

Process.Start()什么都不做

[英]Process.Start() does nothing

I have to start a VPN connection (Fortinet) by code. 我必须通过代码启动VPN连接(Fortinet)。 I have a cmd file that establish the connection. 我有一个建立连接的cmd文件。 If I call the cmd file on the shell it works pretty fine. 如果我在shell上调用cmd文件它可以正常工作。 When I call it via Process.Start it does nothing. 当我通过Process.Start调用它时,它什么也没做。 It doesn't throw any exception, it seems to execute but VPN does not connect. 它不会抛出任何异常,它似乎执行但VPN无法连接。

On the standard output I can read the echo I put on the cmd file (so it is executing the right file). 在标准输出上,我可以读取放在cmd文件上的echo(因此它正在执行正确的文件)。 I launched a ping -d to see when the vpn goes up, when I call it via shell it goes up in a few seconds, via C# it is not. 我启动了一个ping -d来查看vpn何时上升,当我通过shell调用它时,它会在几秒钟后上升,通过C#它不会。

I also tried a sleep(30000) but nothing. 我也试过睡觉(30000),但没有。

My cmd (ConnectFile.cmd): 我的cmd(ConnectFile.cmd):

@echo off
@echo Connecting to VPN
"C:\Program Files (x86)\Fortinet\SslvpnClient\FortiSSLVPNclient.exe" connect -s "vpn myvpn"

My code (connectFile and disconnectFile are strings that contain the full path of the cmd files): 我的代码(connectFile和disconnectFile是包含cmd文件的完整路径的字符串):

try
{
    var startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.FileName = connectFile;
    startInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(connectFile) ?? "";
    System.Diagnostics.Process process = System.Diagnostics.Process.Start(startInfo);
    System.Threading.Thread.Sleep(30000);
    base.GetFiles(folder);
}
finally
{
    System.Diagnostics.Process.Start(disconnectFile);
}

You have to separate the parameters. 你必须分开参数。 FileName is for the exe file, not the whole shell command (you're explicitly saying not to use the shell). FileName用于exe文件,而不是整个shell命令(您明确表示使用shell)。 So put the parameters to the Arguments property in ProcessStartInfo . 因此,将参数放入ProcessStartInfoArguments属性。

In your case, the parameters should be: 在您的情况下,参数应为:

  • FileName - C:\\Program Files (x86)\\Fortinet\\SslvpnClient\\FortiSSLVPNclient.exe (no quotes) FileName - C:\\Program Files (x86)\\Fortinet\\SslvpnClient\\FortiSSLVPNclient.exe (无引号)
  • Arguments - connect -s "vpn myvpn" (again, no quoting) 参数 - connect -s "vpn myvpn" (再次,没有引用)

Second, you have to read the standard output if you capture it. 其次,如果捕获它,则必须读取标准输出。 If the output buffer gets full before you read it, the called process will stop working - it has to wait for the buffer to be emptied. 如果输出缓冲区在读取之前已满,则调用的进程将停止工作 - 它必须等待清空缓冲区。 If you're sure the application will actually finish at some point, simply remove the Thread.Sleep and call ReadToEnd right away. 如果您确定应用程序实际上会在某个时刻完成,只需删除Thread.Sleep并立即调用ReadToEnd Otherwise, use eg. 否则,请使用例如。 asynchronous reading to get the data. 异步读取以获取数据。

Also, it's usually a good idea to set WorkingDirectory . 此外,设置WorkingDirectory通常是个好主意。 Even if the application doesn't need any data from the working directory, it's safer, since only admins can change Program Files - this helps against DLL inject hacks. 即使应用程序不会从工作目录需要的任何数据,它的安全,因为只有管理员可以修改Program Files -这有助于对DLL注入黑客。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM