解决方案 »

  1.   

    我正在用的一个类,人试下。有个拨号方式是包含DOTras的。
    using System;
    using System.Collections.Generic;using System.Text;
    using System.Runtime.InteropServices;
    using System.Net;using System.Text.RegularExpressions;
    using Microsoft.Win32;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;
    using DotRas;namespace REGQQ
    {
        public delegate void Do_adslEventHandler(bool re);    public class AdslTool
        {
            /// <summary>
            /// 重新拨号
            /// </summary>
            public bool CennectioningTF = false; //正在拨号
            public string Cname, User, Pwd;
            public event Do_adslEventHandler do_adslEventHandler = null;        public AdslTool()
            {
                //Cname = QQGlobal.adslname;
                //User = QQGlobal.adsluser;
                //Pwd = QQGlobal.adslpwd;
            }
            public AdslTool(string name, string user, string pwd)
            {
                Cname = name;
                User = user;
                Pwd = pwd;        }        public void adslCennection0()
            {
                try
                {
                    RASDisplay ras = new RASDisplay();
                    ras.Disconnect();
                    System.Threading.Thread.Sleep(QQGlobal.AdslWaitTime * 1000);
                    ras.Connect(Cname);
                    System.Threading.Thread.Sleep(500);
                    do_adslEventHandler(true);            }
                catch (Exception)
                {
                    do_adslEventHandler(false);
                }
            }        public void adslCennection()
            {
                try
                {
                    RASDisplay ras = new RASDisplay();
                    ras.Disconnect();
                    //doProcessDISCONNECT(string.Format(" {0} /DISCONNECT", Cname));
                    System.Threading.Thread.Sleep(QQGlobal.AdslWaitTime * 1000 + 200);
                    bool re = doProcess(string.Format(" {0} {1} {2}", Cname, User, Pwd));
                    Thread.Sleep(200);
                    do_adslEventHandler(re);
                }
                catch (Exception)
                {
                    do_adslEventHandler(false);
                }
            }        //RasDialer方式
            public void adslCennection2()
            {
                //try
                //{
                    doProcessDISCONNECT(string.Format(" {0} /DISCONNECT", Cname));
                    System.Threading.Thread.Sleep(QQGlobal.AdslWaitTime * 1000 + 200);
                    RasDialer dialer = new RasDialer();
               
                    dialer.EntryName = Cname;
                    dialer.PhoneNumber = " ";
                    dialer.AllowUseStoredCredentials = true;
                    dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                    dialer.Timeout = 2000;
                    dialer.Dial();
                    Thread.Sleep(500);
                    do_adslEventHandler(true);
                //}
                //catch (Exception)
                //{
                //    do_adslEventHandler(false);
                //}
            }        private void doProcessDISCONNECT(string str)
            {
                try
                {
                    ProcessStartInfo ras = new ProcessStartInfo();
                    ras.FileName = "rasdial.exe";
                    ras.WindowStyle = ProcessWindowStyle.Hidden;
                    ras.CreateNoWindow = true;
                    ras.Arguments = str;
                    ras.RedirectStandardOutput = true;
                    ras.UseShellExecute = false;
                    Process p = Process.Start(ras);
                    //string re = p.StandardOutput.ReadToEnd();
                }
                catch (Exception)
                {
                }        }
            private void Killprocess()
            {
                System.Diagnostics.Process myproc = new System.Diagnostics.Process();            //得到所有打开的进程
                             foreach (System.Diagnostics.Process singleProc in System.Diagnostics.Process.GetProcesses())
                    {
                        try
                        {
                            string ProcessName = singleProc.ProcessName.ToLower();
                            if (ProcessName.Contains("rasdial"))
                            {
                                singleProc.Kill();
                            }
                        }
                        catch (Exception)
                        {
                          
                        }
                    }
      
            }        private bool doProcess(string str)
            {            //try
                //{
                    bool rebool = false;                Killprocess();
                    ProcessStartInfo ras = new ProcessStartInfo();
                    ras.FileName = "rasdial.exe";
                    ras.WindowStyle = ProcessWindowStyle.Hidden;
                    ras.CreateNoWindow = true;
                    ras.Arguments = str;
                    ras.RedirectStandardOutput = true;
                    ras.UseShellExecute = false;
                    Process p = Process.Start(ras);
                  
                    string re = p.StandardOutput.ReadToEnd();
                    p.Close();
                    p.Dispose();
                    if (re.IndexOf("已连接") > 0)
                        rebool = true;
                    return rebool;
                //}
                //catch (Exception)
                //{
                //    return false;
                //}        }
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            public struct RasEntryName      //define the struct to receive the entry name
            {
                public int dwSize;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256 + 1)]
                public string szEntryName;
    #if WINVER5
         public int dwFlags;
         [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
         public string szPhonebookPath;
    #endif
            }        [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
            );
            //获取当前创建的adsl宽带列表        public string[] adsllist()
            {
                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 = 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 = RasEnumEntries(null, null, names, ref lpSize, out lpNames);
                }
                string[] keysList = new string[names.Length];
                if (lpNames > 0)
                {
                    for (int i = 0; i < names.Length; i++)
                    {
                        keysList[i] = (names[i].szEntryName);
                    }
                }            return keysList;
            }
            [DllImport("wininet.dll")]
            private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);        private static int currentTry = 0;
            public static bool IsConnected(out string ip, out string head)
            {            ip = "";
                head = "";
                //try
                //{
                currentTry++;
                HttpHelper http = new HttpHelper();
                string url = "http://iframe.ip138.com/ic.asp";
                http.timeout = 60000;
                string result = http.GetHtml(url);
                string regexStr = "(?<ip1>[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(?<ip2>\\.[0-9]{1,3})";
                Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
                MatchCollection M = r.Matches(result);
                if (M.Count > 0)
                {
                    ip = M[0].Groups["ip1"].Value + M[0].Groups["ip2"].Value;
                    head = M[0].Groups["ip1"].Value;
                    currentTry = 0;
                    return true;
                }
                else if (currentTry < 3)
                {
                    Thread.Sleep(1000);
                    return IsConnected(out ip, out head);
                }
                else
                {
                    currentTry = 0;
                    return false;
                }
                //}
                //catch (Exception)
                //{            //    return false;
                //}        }        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;
            }
    }
      

  2.   


            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);
                }
            }
        }
      

  3.   


    大哥~~~能给个就单单获取宽带账号的么?WIN7可以获取到的。。我弄了两天 一直失败明天要被老大叼了。。哎。。
      

  4.   


    大哥~~~给个链接地址嘛??这个只能获取ADSL拨号连接~~~~~路由器里面的可以获取到么?
      

  5.   

    http://download.csdn.net/detail/lanzyer/7224331