I am learning C # and I am creating a simple WinForms application, and what it does is launching a simple OpenVPN client:
void button_Connect_Click(object sender, EventArgs e)
{
var proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\OpenVPN\bin";
proc.StartInfo.Arguments = "/c openvpn.exe --config config.ovpn --auto-proxy";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = false;
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
proc.Start();
StreamWriter myStreamWriter = proc.StandardInput;
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
}
void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string Data = e.Data.ToString();
if (Data.Contains("Enter Auth Username"))
{
myStreamWriter("myusername");
}
}
}
Now it sends all CMD output to my program, which run commands depending on the output.
My current problem: I need to write to the stream. I use myStreamWriterin proc_DataReceived, however it is not in the same context, so it does not work.
I get the following error: The name 'myStreamWriter' does not exist in the current contextwhich clearly does not exist in this area.
How do I do this job? Get / set properties? As I said, I am new to C #, so any help is appreciated.
source
share