【发布时间】:2017-03-17 14:33:06
【问题描述】:
问候,
- 操作系统:Windows 7 /64 位
- 应用程序:Visual Studio 2012 / C# 和 DotRas 1.3 库
我对 c# 或 VS 很陌生,所以请多多包涵。经过数小时的研发,我终于用 C# / Dotras 制作了一个 pppoe 拨号程序。该程序有 3 个主要按钮
- 在网络连接中创建/添加 PPPoE Internet Dialer Connection,工作正常
- 拨号键,连接新建的拨号器,工作正常
- 断开连接工作正常
我添加了StatuBox,拨号事件应显示在dotras youtube视频教程中(这是用于vpn,但我的项目是用于pppoe dialer)
StatusBox 不更新拨号事件,如连接/密码错误/已连接等。这是我最终感到困惑的部分。
以下是我的代码。
// Dial Button Action
private void button2_Click_1(object sender, EventArgs e)
{
using (RasDialer dialer = new RasDialer())
{
// I had to add below line to update statusTextBox Manualy , want to get rid of it by adding auto status update
this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connection in progress ...", "{0}\r\n\r\n"));
dialer.EntryName = ("pppoe2");
string username = textBox1.Text;
string passwd = textBox2.Text;
// If username is empty dont connect
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Cancelled. Cannot continue with username/password.", "{0}\r\n"));
MessageBox.Show("Enter username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
dialer.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
dialer.Timeout = 1000;
dialer.AllowUseStoredCredentials = true;
// start dialing,
dialer.Dial();
// If dialer connects successfully update StatuTextbox
this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connected."));
}
}
private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Status Changed"));
}
private void rasDialer1_Error(object sender, System.IO.ErrorEventArgs e)
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "STATUS UPDATE TEXT XYZ"));
}
private void rasDialer1_DialCompleted(object sender, DialCompletedEventArgs e)
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "STATUS UPDATE TEXT XYZ"));
}
任何帮助都将不胜感激。
【问题讨论】:
-
我没有看到任何将
rasDialer1_StateChanged事件处理程序附加到您的拨号器的StateChanged事件的代码。 -
如果你不介意,你能提供一个例子吗?
-
没问题 -
dialer.StateChanged += rasDialer1_StateChanged创建拨号程序后。对Error和DialCompleted执行相同操作。 -
好的,我将代码修改为
dialer.Dial();dialer.StateChanged += rasDialer1_StateChanged;并在Error和DialCompeted中添加了此代码,但仍然拨打状态文本框中未更新的事件。 -
我认为你应该在拨号前设置事件处理程序
标签: c# visual-studio-2012 dotras