C# 实现ADSL自动断网和拨号的方法(适用于拨号用户)
封装类:
usingSystem;
usingSystem.Runtime.InteropServices;
publicstructRASCONN
{
publicintdwSize;
publicIntPtrhrasconn;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=257)]
publicstringszEntryName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=17)]
publicstringszDeviceType;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=129)]
publicstringszDeviceName;
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
publicstructRasStats
{
publicintdwSize;
publicintdwBytesXmited;
publicintdwBytesRcved;
publicintdwFramesXmited;
publicintdwFramesRcved;
publicintdwCrcErr;
publicintdwTimeoutErr;
publicintdwAlignmentErr;
publicintdwHardwareOverrunErr;
publicintdwFramingErr;
publicintdwBufferOverrunErr;
publicintdwCompressionRatioIn;
publicintdwCompressionRatioOut;
publicintdwBps;
publicintdwConnectionDuration;
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
publicstructRasEntryName
{
publicintdwSize;
//[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName+1)]
publicstringszEntryName;
//#ifWINVER5
//publicintdwFlags;
//[MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
//publicstringszPhonebookPath;
//#endif
}
publicclassRAS
{
[DllImport("Rasapi32.dll",EntryPoint="RasEnumConnectionsA",
SetLastError=true)]
internalstaticexternintRasEnumConnections
(
refRASCONNlprasconn,//buffertoreceiveconnectionsdata
refintlpcb,//sizeinbytesofbuffer
refintlpcConnections//numberofconnectionswrittentobuffer
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
internalstaticexternuintRasGetConnectionStatistics(
IntPtrhRasConn,//handletotheconnection
[In,Out]RasStatslpStatistics//buffertoreceivestatistics
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
publicexternstaticuintRasHangUp(
IntPtrhrasconn//handletotheRASconnectiontohangup
);
[DllImport("rasapi32.dll",CharSet=CharSet.Auto)]
publicexternstaticuintRasEnumEntries(
stringreserved,//reserved,mustbeNULL
stringlpszPhonebook,//pointertofullpathand
//filenameofphone-bookfile
[In,Out]RasEntryName[]lprasentryname,//buffertoreceive
//phone-bookentries
refintlpcb,//sizeinbytesofbuffer
outintlpcEntries//numberofentrieswritten
//tobuffer
);
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
publicexternstaticintInternetDial(
IntPtrhwnd,
[In]stringlpszConnectoid,
uintdwFlags,
refintlpdwConnection,
uintdwReserved
);
publicRAS()
{
}
}
publicenumDEL_CACHE_TYPE//要删除的类型。
{
File,//表示internet临时文件
Cookie//表示Cookie
}
publicclassRASDisplay
{
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
publicstaticexternboolDeleteUrlCacheEntry(
DEL_CACHE_TYPEtype
);
privatestringm_duration;
privatestringm_ConnectionName;
privatestring[]m_ConnectionNames;
privatedoublem_TX;
privatedoublem_RX;
privateboolm_connected;
privateIntPtrm_ConnectedRasHandle;
RasStatsstatus=newRasStats();
publicRASDisplay()
{
m_connected=true;
RASlpras=newRAS();
RASCONNlprasConn=newRASCONN();
lprasConn.dwSize=Marshal.SizeOf(typeof(RASCONN));
lprasConn.hrasconn=IntPtr.Zero;
intlpcb=0;
intlpcConnections=0;
intnRet=0;
lpcb=Marshal.SizeOf(typeof(RASCONN));
nRet=RAS.RasEnumConnections(reflprasConn,reflpcb,ref
lpcConnections);
if(nRet!=0)
{
m_connected=false;
return;
}
if(lpcConnections>0)
{
//for(inti=0;i<lpcConnections;i++)
//{
RasStatsstats=newRasStats();
m_ConnectedRasHandle=lprasConn.hrasconn;
RAS.RasGetConnectionStatistics(lprasConn.hrasconn,stats);
m_ConnectionName=lprasConn.szEntryName;
intHours=0;
intMinutes=0;
intSeconds=0;
Hours=((stats.dwConnectionDuration/1000)/3600);
Minutes=((stats.dwConnectionDuration/1000)/60)-(Hours*60);
Seconds=((stats.dwConnectionDuration/1000))-(Minutes*60)-(Hours*3600);
m_duration=Hours+"hours"+Minutes+"minutes"+Seconds+"secs";
m_TX=stats.dwBytesXmited;
m_RX=stats.dwBytesRcved;
//}
}
else
{
m_connected=false;
}
intlpNames=1;
intentryNameSize=0;
intlpSize=0;
RasEntryName[]names=null;
entryNameSize=Marshal.SizeOf(typeof(RasEntryName));
lpSize=lpNames*entryNameSize;
names=newRasEntryName[lpNames];
names[0].dwSize=entryNameSize;
uintretval=RAS.RasEnumEntries(null,null,names,reflpSize,outlpNames);
//ifwehavemorethanoneconnection,weneedtodoitagain
if(lpNames>1)
{
names=newRasEntryName[lpNames];
for(inti=0;i<names.Length;i++)
{
names[i].dwSize=entryNameSize;
}
retval=RAS.RasEnumEntries(null,null,names,reflpSize,outlpNames);
}
m_ConnectionNames=newstring[names.Length];
if(lpNames>0)
{
for(inti=0;i<names.Length;i++)
{
m_ConnectionNames[i]=names[i].szEntryName;
}
}
}
publicstringDuration
{
get
{
returnm_connected?m_duration:"";
}
}
publicstring[]Connections
{
get
{
returnm_ConnectionNames;
}
}
publicdoubleBytesTransmitted
{
get
{
returnm_connected?m_TX:0;
}
}
publicdoubleBytesReceived
{
get
{
returnm_connected?m_RX:0;
}
}
publicstringConnectionName
{
get
{
returnm_connected?m_ConnectionName:"";
}
}
publicboolIsConnected
{
get
{
returnm_connected;
}
}
publicintConnect(stringConnection)
{
inttemp=0;
uintINTERNET_AUTO_DIAL_UNATTENDED=2;
intretVal=RAS.InternetDial(IntPtr.Zero,Connection,INTERNET_AUTO_DIAL_UNATTENDED,reftemp,0);
returnretVal;
}
publicvoidDisconnect()
{
RAS.RasHangUp(m_ConnectedRasHandle);
}
}
调用方法:
RASDisplayras=newRASDisplay();
ras.Disconnect();//断开连接
ras.Connect("ADSL");//重新拨号
注意:断开后连接时,如果不想出现确认对话框,请按下面的步骤进行设置:
1、右击“网上邻居”--属性;
2、在随后出现的“宽带连接”窗口中,右击宽带连接,单击“属性”;
3、在随后出现的属性对话框中,单击“选项”;
4、把随后出现的“提示名称、密码和证书等”前面的对号去掉,点“确定”退出;
以上这篇C#实现ADSL自动断网和拨号的方法(适用于拨号用户)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。