C # implement ADSL dialing

Source: Internet
Author: User
C # implement ADSL dialing at pm
The simplest method is to use the rasdial command.
Rasdial is a Windows Command that is easy to use. In practice, you can save the following code as a batch file and then run it directly or call it in a program.

Rasdial.exe/disconnect
Del "C: \ Documents ents and Settings \ hyuqin \ cookies \ *. *"/Q' clear cookie
Rasdial.exe connection name connection account connection password 'redialing

C # code

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net;
Using system. IO;
Using system. Text. regularexpressions;
Using system. diagnostics;
Using system. Threading;
Using system. runtime;
Using system. runtime. interopservices;

Namespace refreship
{
Public partial class dailer: Form
{
Public dailer ()
{
Initializecomponent ();
}
Private Static mutex = new mutex ();
Private process dailer = new process ();


Private void form1_load (Object sender, eventargs E)
{
This. Visible = true;
}
# Region Functions
Int DESC;
// Creating the extern function...
[Dllimport ("wininet. dll")]
Private extern static bool internetgetconnectedstate (Out int description, int reservedvalue );
// Creating a function that uses the API function...
// If out parameter returns 18 then fail, if 81 then success
Public void isconnectedtointernet ()
{
Internetgetconnectedstate (Out DESC, 0 );
}

Private void stopdailer ()
{
// Process dailer = new process ();
// Dailer. startinfo. filename = "close. Bat ";
// Dailer. Start ();
// Dailer. Close ();
While (DESC = 81)
{
Lock (dailer)
{
If (! Isalive ("rundll32 "))
{
Mutex. waitone ();
Dailer. startinfo. filename = "rundll32.exe ";
Dailer. startinfo. Arguments = "iedkcs32.dll closerasconnections ";
Dailer. Start ();
// Thread. Sleep (1000 );
Mutex. releasemutex ();
}
// Dailer. waitforexit (1000 );
// Dailer. Close ();
}
Isconnectedtointernet ();
}
Dailer. Close ();
}

Private void startdailer ()
{
// Process dailer = new process ();
// Dailer. startinfo. filename = "Connect. Bat ";
// Dailer. Start ();
// Dailer. Close ();
While (DESC! = 81)
{
Lock (dailer)
{
If (! Isalive ("rasdial "))
{
Mutex. waitone ();
Dailer. startinfo. filename = "rasdial.exe ";
Dailer. startinfo. Arguments = txtdail. Text. Trim () + "" + txtname. Text. Trim () + "" + txtpwd. Text. Trim ();
Dailer. Start ();
Mutex. releasemutex ();
}
// Dailer. waitforexit (1000 );
// Dailer. Close ();
// Thread. Sleep (1000 );
}
Isconnectedtointernet ();
}
Dailer. Close ();
}

Private bool isalive (string name)
{
Process [] PS = process. getprocessesbyname (name );
If (PS. length> 0)
{
Return true;
}
Else
{
Return false;
}
}

Private bool validateinput ()
{
If (string. isnullorempty (txtdail. Text. Trim ()))
{
MessageBox. Show ("Enter the bandwidth dialing name! ");
Return false;
}
If (string. isnullorempty (txtname. Text. Trim ()))
{
MessageBox. Show ("Enter the bandwidth dialing user name! ");
Return false;
}
If (string. isnullorempty (txtpwd. Text. Trim ()))
{
MessageBox. Show ("Enter the bandwidth dialing password! ");
Return false;
}
Return true;
}
# Endregion

Private void btnstart_click (Object sender, eventargs E)
{
If (validateinput ())
{
This. Visible = false;
This. policyicon1.icon = new icon ("open. ICO ");
Isconnectedtointernet ();
This. startdailer ();
}
}

Private void exit toolstripmenuitem_click (Object sender, eventargs E)
{
Application. Exit ();
}

Private void button2_click (Object sender, eventargs E)
{
Isconnectedtointernet ();
Stopdailer ();
This. policyicon1.icon = new icon ("close. ICO ");
MessageBox. Show ("the program has been suspended! ");
}

Private void btnmini_click (Object sender, eventargs E)
{
This. Visible = false;
}

Private void set toolstripmenuitem_click (Object sender, eventargs E)
{
This. Visible = true;
}

Private void policyicon#doubleclick (Object sender, eventargs E)
{
This. Visible =! This. visible;
}

Private void btnok_click (Object sender, eventargs E)
{
If (validateinput ())
{
This. Visible = false;
This. policyicon1.icon = new icon ("open. ICO ");
Isconnectedtointernet ();
This. startdailer ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.