1

在我的应用程序中,我通过命令提示符窗口设置了一个 vpn 连接,输出将放在一个文本框中。它会自我刷新,因此当它从命令提示符获取新行时会添加一个新行。

输出将是这样的。

> Microsoft Windows [versie 6.1.7601] Copyright (c) 2009 Microsoft
> Corporation. Alle rechten voorbehouden.
> 
> C:\Users\...\Desktop>rasdial.exe VPN username password
> Verbinding maken met VPN...
> Gebruikersnaam en wachtwoord controleren...
> Uw computer wordt in het netwerk geregistreerd...
> Verbinding gemaakt met VPN Opdracht voltooid.
> 
> C:\Users\Helpdesk\Desktop>exit

我怎样才能删除 Microsoft Windows 部分,所以我只有这个。

rasdial.exe VPN username password
Verbinding maken met VPN...
Gebruikersnaam en wachtwoord controleren...
Uw computer wordt in het netwerk geregistreerd...
Verbinding gemaakt met VPN Opdracht voltooid.

这是我添加单独行的代码。

private void ConnectVPN()
    {
        CheckForIllegalCrossThreadCalls = false;
        Process CMDprocess = new Process();
        System.Diagnostics.ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo();
        StartInfo.FileName = "cmd";
        StartInfo.CreateNoWindow = true;
        StartInfo.RedirectStandardInput = true;
        StartInfo.RedirectStandardOutput = true;
        StartInfo.UseShellExecute = false;
        CMDprocess.StartInfo = StartInfo;
        CMDprocess.Start();
        System.IO.StreamReader SR = CMDprocess.StandardOutput;
        System.IO.StreamWriter SW = CMDprocess.StandardInput;
        SW.WriteLine("rasdial.exe " + comboBox1.Text + " " + loginText.Text + " " + wachtwoordText.Text);
        SW.WriteLine("exit");
        string line = null;
        do
        {
            line = SR.ReadLine();
            if ((line != null))
            {
                VerbindingOutput.Text = VerbindingOutput.Text + line + Environment.NewLine;
            }
        } while (!(line == null));
    }
4

3 回答 3

2

直接启动 rasdial 进程,然后使用 Arguments 属性传递命令行参数:

StartInfo.Arguments = comboBox1.Text + " " + loginText.Text + " " + wachtwoordText.Text;
于 2012-09-27T20:54:59.493 回答
1

不要将密码作为命令参数传递,因为它会引发安全问题。

您的示例中的这段代码添加了一个条件来确定一行是否不是StartsWith特定的字符串。

do
{
    line = SR.ReadLine();

    if ((line != null))
    {
        if(!line.StartsWith("Microsoft Windows", StringComparison.OrdinalIgnoreCase))
        {
            VerbindingOutput.Text = VerbindingOutput.Text + line + Environment.NewLine;
        }
    }
} while (line != null);
于 2012-09-27T20:55:54.850 回答
1

为什么不像 PandaNL 所说的那样直接 shell rasdial.exe,然后将您的标准输出事件重定向到另一个函数?通过这种方式,您可以明确地观察线路上的每一行并相应地对其进行格式化,并将其附加到流编写器中。这是一些伪代码:

            string Executable = "C:\\*******";

            using (Process p = new Process())
            {
                // Redirect the output stream of the child process. 
                p.StartInfo.FileName = Executable;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;  //must be true to grab event
                p.StartInfo.CreateNoWindow = true;  //false if you want to see the command window
                p.EnableRaisingEvents = true;   //must be true to grab event
                p.OutputDataReceived += p_WriteData;   //setup your output handler
                p.Start();
                p.BeginOutputReadLine();
            }

private void p_WriteData(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
         string FixedOutput = MethodToFormatOutput(e.Data);  //do string manipulation here
          using(StreamWriter SW = new StreamWriter("C:\\output.txt",true)
            {
              SW.WriteLine(FixedOutput);
            }
        }
    }
于 2012-09-27T21:09:11.777 回答