C#.NET獲取撥號(hào)連接的寬帶連接方法
本文實(shí)例講述了C#.NET獲取撥號(hào)連接的寬帶連接方法。分享給大家供大家參考。具體如下:
該代碼直接可以用,我在XP VS2010 NET3.5上測(cè)試通過(guò)。
首先是ASDL的封裝
class SinASDL
{
//ASDL在注冊(cè)表中的存放位置,這個(gè)是針對(duì)WinXP的,
//不知道Win7是否是這個(gè),待驗(yàn)證
private static String _adlskeys = @"RemoteAccess\Profile";
public static String adlskeys
{
get
{
return _adlskeys;
}
}
/// <summary>
/// 獲取本機(jī)的撥號(hào)名稱(chēng),也就是本機(jī)上所有的撥號(hào)
/// </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>
/// 實(shí)例化一個(gè)ASDL連接
/// </summary>
/// <param name="asdlname">ASDL名稱(chēng),如“寬帶連接”</param>
/// <param name="username">用戶(hù)名</param>
/// <param name="password">密碼</param>
/// <param name="windowstyle">窗口顯示方式,默認(rèn)為因此撥號(hào)過(guò)程</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>
/// 撥號(hào)名稱(chēng)
/// </summary>
public String ASDLName
{
get
{
return this._asdlname;
}
set
{
this._asdlname = value;
}
}
/// <summary>
/// 撥號(hào)進(jìn)程的窗口方式
/// </summary>
public ProcessWindowStyle WindowStyle
{
get
{
return this._windowstyle;
}
set
{
this._windowstyle = value;
}
}
private String _username = null; //用戶(hù)名
private String _password = null; //密碼
/// <summary>
/// 用戶(hù)名
/// </summary>
public String Username
{
get
{
return this._username;
}
set
{
this._username = value;
}
}
/// <summary>
/// 密碼
/// </summary>
public String Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
/// <summary>
/// 開(kāi)始撥號(hào)
/// </summary>
/// <returns>返回?fù)芴?hào)進(jìn)程的返回值</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>
/// 端口連接
/// </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;
}
}
下面是使用測(cè)試:
//SinASDL asdl = new SinASDL("寬帶連接", "08793312221", "123456");
//寬帶連接
//使用枚舉到的第一個(gè)進(jìn)行撥號(hào)
SinASDL asdl = new SinASDL(SinASDL.GetASDLNames()[0], "08793312221", "123456", System.Diagnostics.ProcessWindowStyle.Normal);
if (asdl.Connect() == 0)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Fail");
}
我自己測(cè)試的時(shí)候是通過(guò)的。
如果電腦上不止一個(gè)撥號(hào)的,那么你可以用SinASDL.GetASDLNames()進(jìn)行枚舉。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
c# 成員類(lèi)型訪問(wèn)權(quán)限低于字段本身的實(shí)現(xiàn)
本文主要介紹了c# 成員類(lèi)型訪問(wèn)權(quán)限低于字段本身的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
將DLL放入到資源中,運(yùn)行時(shí)自動(dòng)加載的小例子
這篇文章介紹了將DLL放入到資源中,運(yùn)行時(shí)自動(dòng)加載的小例子,有需要的朋友可以參考一下2013-10-10
C#/VB.NET實(shí)現(xiàn)在PDF文檔中創(chuàng)建表格
表格是一種直觀高效的數(shù)據(jù)展示方式,可以按行和列的形式呈現(xiàn)數(shù)據(jù),從而更容易吸引讀者的注意,本文將介紹如何使用 Spire.PDF for .NET 通過(guò) .NET 程序在 PDF 文檔中創(chuàng)建表格,需要的可以參考下2023-12-12
C# 整數(shù)轉(zhuǎn)二進(jìn)制字符串方式
這篇文章主要介紹了C# 整數(shù)轉(zhuǎn)二進(jìn)制字符串方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
WPF應(yīng)用啟動(dòng)慢的問(wèn)題解決
今天碰到一個(gè)奇怪的現(xiàn)象,在某些機(jī)器上,進(jìn)行了系統(tǒng)還原后,WPF應(yīng)用打開(kāi)較慢,約有35s。本文先記錄下該問(wèn)題的解決方案,應(yīng)用啟動(dòng)性能官方文檔中有說(shuō)明,還有搜到的其它方案沒(méi)來(lái)得及測(cè)試,如NGEN update2021-05-05

