[源码、文档、分享] [原创]mobile联系人和短信操作 C#(转)

mobile联系人和短信操作
智能机的普及程度已经很高了  手机能为我们带来什么!!!!!!!!!! 复制内容到剪贴板
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsMobile.PocketOutlook;



using System.IO;


using MAPIdotnet;

namespace DeviceApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void ContactClear()  // 清空电话簿
        {
            using (OutlookSession outlookSession = new OutlookSession())//创建OutlookSession 实例
            {
                outlookSession.Contacts.Items.Clear();//清空电话簿
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes != MessageBox.Show("您确定要清空联系人吗?", "确认", MessageBoxButtons.YesNo,
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2))
                return;

            ContactClear();
            MessageBox.Show("成功执行!", "清空联系人");//通知用户,成功执行
        }

        void SMSBackup(string fileName)  // 将短信息备份到指定文件
        {
            try
            {
                File.Delete(fileName);//删除文件
            }
            catch
            {
            }
            using (MAPI mapi = new MAPI())
            {
                IMAPIMsgStore[] messageStores = mapi.MessageStores;
                foreach (IMAPIMsgStore store in messageStores)
                {
                    IMAPIFolderID inboxid = store.ReceiveFolder;//"收件箱"文件夹ID
                    IMAPIFolder inbox = store.OpenFolder(inboxid);//打开收件箱。其他文件夹也可使用类似方式打开
                    IMAPIMessage[] messages = inbox.GetNextMessages((int)inbox.NumSubItems);
                    using (StreamWriter sw = File.AppendText(fileName))//打开文件
                    {
                        foreach (IMAPIMessage msg in messages)//轮询SMS
                        {
                            sw.WriteLine("{0}:{1}", msg.Sender.FullAddress, msg.Subject);//将发件人,信息内容存入文件。这里仅保存发件人,信息内容。更多内容可自行添加
                        }
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == saveFileDialog1.ShowDialog())//用户确认选择了一个文件
            {
                string fileName = saveFileDialog1.FileName;//获取用户所选择的文件名
                SMSBackup(saveFileDialog1.FileName);//将短信息备份到指定文件
                MessageBox.Show("成功执行!", "备份短信息");//通知用户,成功执行
            }
        }

        void SMSClear()// 清空短信息
        {
            using (MAPI mapi = new MAPI())
            {
                IMAPIMsgStore[] messageStores = mapi.MessageStores;
                foreach (IMAPIMsgStore store in messageStores)
                {
                    IMAPIFolderID inboxid = store.ReceiveFolder;//"收件箱"文件夹ID
                    IMAPIFolder inbox = store.OpenFolder(inboxid);//打开收件箱。其他文件夹也可使用类似方式打开
                    inbox.EmptyFolder();
                    //IMAPIMessage[] messages = inbox.GetNextMessages((int)inbox.NumSubItems);
                    //foreach (IMAPIMessage msg in messages)//轮询SMS
                    //{
                    //    inbox.DeleteMessage(msg.MessageID);//删除SMS
                    //}
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes != MessageBox.Show("您确定要清空短信息吗?", "确认", MessageBoxButtons.YesNo,
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2))
                return;

            SMSClear();
            MessageBox.Show("成功执行!", "清空短信息");//通知用户,成功执行
        }


        void ContactBackup(string fileName)   // 将通讯簿备份到指定文件
        {
            try
            {
                File.Delete(fileName);//删除文件
            }
            catch
            {
            }
            using (OutlookSession s = new OutlookSession())//创建OutlookSession实例
            {
                using (StreamWriter sw = File.AppendText(fileName))//打开文件
                {
                    foreach (Contact c in s.Contacts.Items)//轮询通讯簿
                    {
                        sw.WriteLine("{0}:{1}", c.FileAs, c.MobileTelephoneNumber);//将姓名,手机号存入附件文件。这里仅保存姓名,手机号。更多内容可自行添加
                    }
                    sw.Flush();
                    sw.Close();
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == saveFileDialog1.ShowDialog())//用户确认选择了一个文件
            {
                string fileName = saveFileDialog1.FileName;//获取用户所选择的文件名
                ContactBackup(saveFileDialog1.FileName);//将通讯簿备份到指定文件
                MessageBox.Show("成功执行!", "备份联系人");//通知用户,成功执行
            }
        }
    }
}
posted @ 2011-05-09 11:43  董雨  阅读(262)  评论(0编辑  收藏  举报