C# 从文本框中删除特定文本

C# 从文本框中删除特定文本,c#,.net,C#,.net,在我的应用程序中,我通过命令提示符窗口设置vpn连接,输出将放在文本框中。它会刷新自身,以便在从命令提示符获取新行时添加新行 输出将如下所示 > Microsoft Windows [versie 6.1.7601] Copyright (c) 2009 Microsoft > Corporation. Alle rechten voorbehouden. > > C:\Users\...\Desktop>rasdial.exe VPN username pass

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

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

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

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

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

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

示例中的这段代码添加了一个条件,以确定某行是否未使用特定字符串启动

do
{
    line = SR.ReadLine();

    if ((line != null))
    {
        if(!line.StartsWith("Microsoft Windows", StringComparison.OrdinalIgnoreCase))
        {
            VerbindingOutput.Text = VerbindingOutput.Text + line + Environment.NewLine;
        }
    }
} while (line != null);

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

示例中的这段代码添加了一个条件,以确定某行是否未使用特定字符串启动

do
{
    line = SR.ReadLine();

    if ((line != null))
    {
        if(!line.StartsWith("Microsoft Windows", StringComparison.OrdinalIgnoreCase))
        {
            VerbindingOutput.Text = VerbindingOutput.Text + line + Environment.NewLine;
        }
    }
} while (line != null);

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

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

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

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

直接shell rasdial.exe而不是shell命令。您的意思是StartInfo.FileName=“rasdial.exe”;但是我怎样才能给它诸如VPn登录名和密码之类的参数呢?与其去外壳化命令,不如直接外壳化rasdial.exe。你是说StartInfo.FileName=“rasdial.exe”;但我如何才能给它的参数,如VPn登录名和密码?安全问题!对其进行沙箱处理,并在任务管理器中观察命令行列中“进程”选项卡下的进程,您将看到密码值。从理论上讲,如果系统遭到破坏,恶意程序可能会转储进程信息,并且这些凭据将向全世界提供。只有3个人会使用此应用程序,因此我将承担此风险。安全问题!对其进行沙箱处理,并在任务管理器中观察命令行列中“进程”选项卡下的进程,您将看到密码值。从理论上讲,如果系统遭到破坏,恶意程序可能会转储进程信息,并且这些凭据将向全世界提供。只有3个人会使用此应用程序,因此我将承担此风险。