当前位置:首页 > 电脑百科 > 电脑编程 > 手机访问:m.cnwxk.com

c#读取mobile手机联系人和SIM卡联系人之示例-电脑编程

来源:www.cnwxk.com时间:2017-07-26编辑:中国文学库最记录:手机版

c#读取mobile手机联系人和SIM卡联系人的示例

//////////////////手机联系人操作开始 ////////////////// 

using Microsoft.WindowsMobile;

using Microsoft.WindowsMobile.PocketOutlook;

OutlookSession myoutlookSession = new OutlookSession();

                String[] items = new String[2];
                foreach (Contact c in myoutlookSession.Contacts.Items)
                {

                    list.Add(new string[2] { c.FileAs, c.MobileTelephoneNumber });
                }

//////////////////手机联系人操作开始 ////////////////// 


//////////////////SIM卡联系人操作类开始 ////////////////// 

    /// <summary>
    /// SIM卡联系人操作类
    /// </summary>
    public class SIMContactManage
    {
        private const Int64 S_OK = 0x00000000;
        public const int SIM_CAPSTYPE_ALL = 0x3F; // 所有联系人
        public const int SIM_PBSTORAGE_SIM = 0x10; //
        public const int SIM_SMSSTORAGE_SIM = 0x2; //

        [StructLayout(LayoutKind.Sequential)]
        public struct SIMPHONEBOOKENTRY
        {
            public uint cbSize; //
            public uint dwParams; //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string lpszAddress; // 联系人电话
            public uint dwAddressType; //
            public uint dwNumPlan; //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string lpszText; // 联系人姓名
        }

        [DllImport("cellcore.dll")]
        public static extern int SimInitialize(uint dwFlags,
        int lpfnCallBack, uint dwParam, ref int lphSim);
        [DllImport("cellcore.dll")]
        public static extern int SimGetPhonebookStatus(int hSim,
        uint dwLocation, ref uint lpdwUsed, ref uint lpdwTotal);
        [DllImport("cellcore.dll")]
        public static extern int SimReadPhonebookEntry(int hSim, uint dwLocation, uint dwIndex, ref SIMPHONEBOOKENTRY entry);
        [DllImport("cellcore.dll", SetLastError = true)]
        public static extern int SimDeinitialize(int hSim);

        /// <summary>
        /// 获取SIM卡联系人信息
        /// </summary>
        /// <returns></returns>
        public static List<string[]> GetSIMContactList()
        {
            int hSim = 0;
            List<string[]> list = new List<string[]>();
            try
            {
                int result = SimInitialize(0, 0, 0, ref hSim);
                if (result != 0)
                    throw new Exception("SIM打卡失败,请检测SIM是否安装!");
                uint uiUsed = 0;
                uint uiTotal = 0;
                result = SimGetPhonebookStatus(hSim, SIM_PBSTORAGE_SIM, ref uiUsed, ref uiTotal);

                for (int i = 1; i <= uiUsed; i++)
                {
                    SIMPHONEBOOKENTRY entry = new SIMPHONEBOOKENTRY();
                    entry.cbSize = (uint)Marshal.SizeOf(typeof(SIMPHONEBOOKENTRY));
                    result = SimReadPhonebookEntry(hSim, SIM_PBSTORAGE_SIM, (uint)i, ref entry);
                    list.Add(new string[2] {entry.lpszText.Trim(),entry.lpszAddress.Trim() });
                }
                return list;

            }
            catch
            {
                throw;
            }
            finally
            {
                SimDeinitialize(hSim);
               
            }
        }

    }

//////////////////SIM卡联系人操作类结束 ////////////////// 

//////////////////调用方法开始//////////////////

List<string[]> list = SIMContactManage.GetSIMContactList();
           
            string[] str;

            this.lstInfo.BeginUpdate();

            this.lstInfo.Items.Clear();

            for (int i = 0; i < list.Count; i++)
            {
                str = list[i];
                this.lstInfo.Items.Add(new ListViewItem(new String[] { str[0].ToString(), str[1].ToString() }));
            }

            this.lstInfo.EndUpdate();

//注:lstInfo为ListView 控件

//////////////////调用方法结束//////////////////


文章: c#读取mobile手机联系人和SIM卡联系人之示例


  • 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)

    本月排行