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 が述べたように 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 に答える