PandaNL
PandaNL

Reputation: 848

Remove specific text from textbox

In my application i set up a vpn connection via a command prompt window, the output will be put in a textbox. It refreshes itself so it will add a new line when it get one from the command prompt.

The output will be like this.

> 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

how can i remove the Microsoft Windows part, so i only have this.

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.

This is my code for the seperate line adding.

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

Upvotes: 1

Views: 369

Answers (3)

Nicodemeus
Nicodemeus

Reputation: 4083

Don't pass the password as a command argument as it would raise a security issue.

This block of code from your sample adds a condition to determine if a line doesn't StartsWith a specific string.

do
{
    line = SR.ReadLine();

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

Upvotes: 1

Lee Harrison
Lee Harrison

Reputation: 2453

Why not shell rasdial.exe directly as PandaNL stated, then redirect your standard output event to another function? This way you can explictly watch every line that comes across the wire and format it accordingly, and append it with a stream writer. here's some psuedo-code:

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

Upvotes: 1

John Koerner
John Koerner

Reputation: 38079

Start the rasdial process directly and then pass it command line arguments using the Arguments property:

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

Upvotes: 2

Related Questions