欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 实现ADSL自动断网和拨号的方法(适用于拨号用户)

程序员文章站 2022-06-22 16:02:22
封装类: using system; using system.runtime.interopservices; public struct rascon...

封装类:

using system;
using system.runtime.interopservices;

public struct rasconn
{
  public int dwsize;
  public intptr hrasconn;
  [marshalas(unmanagedtype.byvaltstr, sizeconst = 257)]
  public string szentryname;
  [marshalas(unmanagedtype.byvaltstr, sizeconst = 17)]
  public string szdevicetype;
  [marshalas(unmanagedtype.byvaltstr, sizeconst = 129)]
  public string szdevicename;
}

[structlayout(layoutkind.sequential, charset = charset.auto)]
public struct rasstats
{
  public int dwsize;
  public int dwbytesxmited;
  public int dwbytesrcved;
  public int dwframesxmited;
  public int dwframesrcved;
  public int dwcrcerr;
  public int dwtimeouterr;
  public int dwalignmenterr;
  public int dwhardwareoverrunerr;
  public int dwframingerr;
  public int dwbufferoverrunerr;
  public int dwcompressionratioin;
  public int dwcompressionratioout;
  public int dwbps;
  public int dwconnectionduration;
}

[structlayout(layoutkind.sequential, charset = charset.auto)]
public struct rasentryname
{
  public int dwsize;
  //[marshalas(unmanagedtype.byvaltstr,sizeconst=(int)rasfieldsizeconstants.ras_maxentryname + 1)]
  public string szentryname;
  //#if winver5
  // public int dwflags;
  // [marshalas(unmanagedtype.byvaltstr,sizeconst=260+1)]
  // public string szphonebookpath;
  //#endif
}
public class ras
{
  [dllimport("rasapi32.dll", entrypoint = "rasenumconnectionsa",
     setlasterror = true)]

  internal static extern int rasenumconnections
    (
    ref rasconn lprasconn, // buffer to receive connections data
    ref int lpcb, // size in bytes of buffer
    ref int lpcconnections // number of connections written to buffer
    );


  [dllimport("rasapi32.dll", charset = charset.auto)]
  internal static extern uint rasgetconnectionstatistics(
    intptr hrasconn,    // handle to the connection
    [in, out]rasstats lpstatistics // buffer to receive statistics
    );
  [dllimport("rasapi32.dll", charset = charset.auto)]
  public extern static uint rashangup(
    intptr hrasconn // handle to the ras connection to hang up
    );

  [dllimport("rasapi32.dll", charset = charset.auto)]
  public extern static uint rasenumentries(
    string reserved,       // reserved, must be null
    string lpszphonebook,     // pointer to full path and
    // file name of phone-book file
    [in, out]rasentryname[] lprasentryname, // buffer to receive
    // phone-book entries
    ref int lpcb,         // size in bytes of buffer
    out int lpcentries       // number of entries written
    // to buffer
    );

  [dllimport("wininet.dll", charset = charset.auto)]
  public extern static int internetdial(
    intptr hwnd,
    [in]string lpszconnectoid,
    uint dwflags,
    ref int lpdwconnection,
    uint dwreserved
    );

  public ras()
  {
  }
}
public enum del_cache_type //要删除的类型。
{
  file,//表示internet临时文件
  cookie //表示cookie
}

public class rasdisplay
{
  [dllimport("wininet.dll", charset = charset.auto)]
  public static extern bool deleteurlcacheentry(
    del_cache_type type
    );
  private string m_duration;
  private string m_connectionname;
  private string[] m_connectionnames;
  private double m_tx;
  private double m_rx;
  private bool m_connected;
  private intptr m_connectedrashandle;

  rasstats status = new rasstats();
  public rasdisplay()
  {
    m_connected = true;

    ras lpras = new ras();
    rasconn lprasconn = new rasconn();

    lprasconn.dwsize = marshal.sizeof(typeof(rasconn));
    lprasconn.hrasconn = intptr.zero;

    int lpcb = 0;
    int lpcconnections = 0;
    int nret = 0;
    lpcb = marshal.sizeof(typeof(rasconn));

    nret = ras.rasenumconnections(ref lprasconn, ref lpcb, ref
      lpcconnections);

    if (nret != 0)
    {
      m_connected = false;
      return;

    }

    if (lpcconnections > 0)
    {
      //for (int i = 0; i < lpcconnections; i++)

      //{
      rasstats stats = new rasstats();

      m_connectedrashandle = lprasconn.hrasconn;
      ras.rasgetconnectionstatistics(lprasconn.hrasconn, stats);


      m_connectionname = lprasconn.szentryname;

      int hours = 0;
      int minutes = 0;
      int seconds = 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;
    }


    int lpnames = 1;
    int entrynamesize = 0;
    int lpsize = 0;
    rasentryname[] names = null;

    entrynamesize = marshal.sizeof(typeof(rasentryname));
    lpsize = lpnames * entrynamesize;

    names = new rasentryname[lpnames];
    names[0].dwsize = entrynamesize;

    uint retval = ras.rasenumentries(null, null, names, ref lpsize, out lpnames);

    //if we have more than one connection, we need to do it again
    if (lpnames > 1)
    {
      names = new rasentryname[lpnames];
      for (int i = 0; i < names.length; i++)
      {
        names[i].dwsize = entrynamesize;
      }

      retval = ras.rasenumentries(null, null, names, ref lpsize, out lpnames);

    }
    m_connectionnames = new string[names.length];


    if (lpnames > 0)
    {
      for (int i = 0; i < names.length; i++)
      {
        m_connectionnames[i] = names[i].szentryname;
      }
    }
  }

  public string duration
  {
    get
    {
      return m_connected ? m_duration : "";
    }
  }

  public string[] connections
  {
    get
    {
      return m_connectionnames;
    }
  }

  public double bytestransmitted
  {
    get
    {
      return m_connected ? m_tx : 0;
    }
  }
  public double bytesreceived
  {
    get
    {
      return m_connected ? m_rx : 0;

    }
  }
  public string connectionname
  {
    get
    {
      return m_connected ? m_connectionname : "";
    }
  }
  public bool isconnected
  {
    get
    {
      return m_connected;
    }
  }

  public int connect(string connection)
  {
    int temp = 0;
    uint internet_auto_dial_unattended = 2;
    int retval = ras.internetdial(intptr.zero, connection, internet_auto_dial_unattended, ref temp, 0);
    return retval;
  }
  public void disconnect()
  {
    ras.rashangup(m_connectedrashandle);
  }
}

调用方法:

rasdisplay ras = new rasdisplay();
ras.disconnect();//断开连接
ras.connect("adsl");//重新拨号

注意:断开后连接时,如果不想出现确认对话框,请按下面的步骤进行设置:

1、右击“网上邻居”--属性;

2、在随后出现的“宽带连接”窗口中,右击宽带连接,单击“属性”;

3、在随后出现的属性对话框中,单击“选项”;

4、把随后出现的“提示名称、密码和证书等”前面的对号去掉,点“确定”退出;

以上这篇c# 实现adsl自动断网和拨号的方法(适用于拨号用户)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。