C.NET Get Broadband Connection Method for Dial up Connection

  • 2021-06-29 11:52:48
  • OfStack

This paper gives an example of how C#.NET acquires a broadband connection for dial-up connections.Share it for your reference.Specifically as follows:

This code is directly usable and I passed the test on XP VS2010 NET3.5.

First is the encapsulation of ASDL


class SinASDL
{
  //ASDL In the registry, this is for WinXP Of, 
  // I do not know! Win7 Is this true, to be verified 
  private static String _adlskeys = @"RemoteAccess\Profile";
  public static String adlskeys
  {
    get
    {
      return _adlskeys;
    }
  }
  /// <summary>
  ///  Gets the dial name of the local computer, that is, all dials on the local computer 
  /// </summary>
  /// <returns></returns>
  public static String[] GetASDLNames()
  {
    RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(adlskeys);
    if (RegKey != null)
      return RegKey.GetSubKeyNames();
    else
      return null;
  }
  private String _asdlname = null;
  private ProcessWindowStyle _windowstyle = ProcessWindowStyle.Hidden;
  /// <summary>
  ///  instantiation 1 individual ASDL Connect 
  /// </summary>
  /// <param name="asdlname">ASDL Name, such as Broadband Connection </param>
  /// <param name="username"> User name </param>
  /// <param name="password"> Password </param>
  /// <param name="windowstyle"> Window display, defaults to this dialing process </param>
  public SinASDL(String asdlname, String username = null, String password = null, ProcessWindowStyle windowstyle = ProcessWindowStyle.Hidden)
  {
    this.ASDLName = asdlname;
    this.Username = username;
    this.Password = password;
    this.WindowStyle = windowstyle;
  }
  /// <summary>
  ///  Dial name 
  /// </summary>
  public String ASDLName
  {
    get
    {
      return this._asdlname;
    }
    set
    {
      this._asdlname = value;
    }
  }
  /// <summary>
  ///  Dial-up process window mode 
  /// </summary>
  public ProcessWindowStyle WindowStyle
  {
    get
    {
      return this._windowstyle;
    }
    set
    {
      this._windowstyle = value;
    }
  }
  private String _username = null;  // User name 
  private String _password = null;  // Password 
  /// <summary>
  ///  User name 
  /// </summary>
  public String Username
  {
    get
    {
      return this._username;
    }
    set
    {
      this._username = value;
    }
  }
  /// <summary>
  ///  Password 
  /// </summary>
  public String Password
  {
    get
    {
      return this._password;
    }
    set
    {
      this._password = value;
    }
  }
  /// <summary>
  ///  Start dialing 
  /// </summary>
  /// <returns> Returns the return value of the dialing process </returns>
  public int Connect()
  {
    Process pro = new Process();
    pro.StartInfo.FileName = "rasdial.exe";
    pro.StartInfo.Arguments = this.ASDLName + " " + this.Username + " " + this.Password;
    pro.StartInfo.WindowStyle = this.WindowStyle;
    pro.Start();
    pro.WaitForExit();
    return pro.ExitCode;
  }
  /// <summary>
  ///  Port Connection 
  /// </summary>
  /// <returns></returns>
  public int Disconnect()
  {
    Process pro = new Process();
    pro.StartInfo.FileName = "rasdial.exe";
    pro.StartInfo.Arguments = this.ASDLName + " /DISCONNECT";
    pro.StartInfo.WindowStyle = this.WindowStyle;
    pro.Start();
    pro.WaitForExit();
    return pro.ExitCode;
  }
}

Here are the usage tests:


//SinASDL asdl = new SinASDL(" broadband connection ", "08793312221", "123456");
// broadband connection 
// Use the first of the enumerations 1 Dialing 
SinASDL asdl = new SinASDL(SinASDL.GetASDLNames()[0], "08793312221", "123456", System.Diagnostics.ProcessWindowStyle.Normal);
if (asdl.Connect() == 0)
{
  MessageBox.Show("Success");
}
else
{
  MessageBox.Show("Fail");
}

I passed the test myself.

If you have more than one dial-up on your computer, you can enumerate with SinASDL.GetASDLNames().

I hope that the description in this paper will be helpful to everyone's C#program design.


Related articles: