打印本文 打印本文 关闭窗口 关闭窗口
c# 获取宽带名称
作者:佚名  文章来源:本站原创  点击数1521  更新时间:2012/8/28 16:16:42  文章录入:mintao  责任编辑:mintao
c# 获取宽带名称

以下是代码片段:
#region 获得宽带连接名称
        public static List<string> GetPppoeConnectionNames()
        {
            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);
            }

            if (lpNames < 1)
            {
                return null;
            }

            List<string> list = new List<string>();
            foreach (var item in names)
            {
                list.Add(item.szEntryName);
            }
            return list;
        }
        const int RAS_MaxEntryName = 256;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct RasEntryName      //define the struct to receive the entry name
        {
            public int dwSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]

            public string szEntryName;
        }

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

        #endregion

 

private void button1_Click(object sender, EventArgs e)
        {
            //调用方法
            List<string> list = GetPppoeConnectionNames();
            if (list != null)
            {
                foreach (var name in list)
                {
                    richTextBox1.AppendText(name + Environment.NewLine);
                }
            }
 
        }

打印本文 打印本文 关闭窗口 关闭窗口