从文本框中删除特定文本

时间:2012-09-27 20:39:02

标签: c# .net

在我的应用程序中,我通过命令提示符窗口设置了一个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));
    }

3 个答案:

答案 0 :(得分:2)

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

StartInfo.Arguments = comboBox1.Text + " " + loginText.Text + " " + wachtwoordText.Text;

答案 1 :(得分: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);

答案 2 :(得分:1)

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

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