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("成功執行!", "備份連絡人");//通知使用者,成功執行
}
}
}
}