[源碼、文檔、分享] [原創]mobile連絡人和簡訊操作 C#)

來源:互聯網
上載者:User
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("成功執行!", "備份連絡人");//通知使用者,成功執行
            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.