?? windows mobile短信收件夾、發(fā)件夾數(shù)據(jù)讀取.txt
字號:
在Windows Mobile 開發(fā)過程中,很多時(shí)候需要讀取短信收件夾及發(fā)件夾里的數(shù)據(jù),當(dāng)然C#是很難實(shí)現(xiàn)這個(gè)的,因?yàn)槲④洓]有對底層API進(jìn)行封裝,此時(shí),C++又出來了,通過C++封裝一個(gè)DLL,然后在C#中調(diào)用即可(沒辦法,C++)總是那么強(qiáng)。
具體的C++封裝我這里不詳細(xì)介紹,其實(shí)也不是我封裝的,是別人寫的,這里引用過來。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
public class NetMAPI:IDisposable
{
protected IntPtr pMAPI;
public NetMAPI()
{
pMAPI = IntPtr.Zero;
}
~NetMAPI()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
Logout();
}
public IntPtr MAPI
{
get { return pMAPI; }
}
public bool Login()
{
if (pMAPI == IntPtr.Zero) pMAPI = MAPILogin();
return (pMAPI != IntPtr.Zero);
}
public void Logout()
{
if (pMAPI != IntPtr.Zero)
{
MAPILogout(pMAPI);
pMAPI = IntPtr.Zero;
}
}
public bool OpenMessageStore()
{
return MAPIOpenMessageStore(pMAPI);
}
public bool GetNextMessage(out SmsMessage message, bool bUnreadOnly)
{
IntPtr pMessage;
message = null;
if (MAPIGetNextMessage(pMAPI, out pMessage, bUnreadOnly))
{
message = new SmsMessage(pMessage);
}
return (message != null);
}
public bool GetContents()
{
return GetContents(IntPtr.Zero);
}
public bool OpenInbox()
{
return MAPIOpenInbox(pMAPI);
}
/// <summary>
/// Opens the Outbox folder
/// </summary>
/// <returns>true on success</returns>
public bool OpenOutbox()
{
return MAPIOpenOutbox(pMAPI);
}
/// <summary>
/// Opens the Sent Items folder
/// </summary>
/// <returns>true on success</returns>
public bool OpenSentItems()
{
return MAPIOpenSentItems(pMAPI);
}
/// <summary>
/// Opens the Deleted Items folder
/// </summary>
/// <returns>true on success</returns>
public bool OpenDeletedItems()
{
return MAPIOpenDeletedItems(pMAPI);
}
/// <summary>
/// Opens the Drafts folder
/// </summary>
/// <returns>true on success</returns>
public bool OpenDrafts()
{
return MAPIOpenDrafts(pMAPI);
}
/// <summary>
/// Opens the contents of a specific folder
/// </summary>
/// <param name="pFolder"></param>
/// <returns>true on success</returns>
public bool GetContents(IntPtr pFolder)
{
return MAPIGetContents(pMAPI, pFolder);
}
public int GetRowCounts()
{
return MAPIGetRowCount(pMAPI);
}
[DllImport("ReadSMS.dll")]
public static extern bool MAPIInit();
[DllImport("ReadSMS.dll", EntryPoint = "MAPITerm")]
public static extern void Term();
// Profiles, Message Store
[DllImport("ReadSMS.dll")]
protected static extern IntPtr MAPILogin();
[DllImport("ReadSMS.dll")]
protected static extern void MAPILogout(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIOpenMessageStore(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIGetContents(IntPtr pMAPI, IntPtr pFolder);
[DllImport("ReadSMS.dll")]
protected static extern int MAPIGetRowCount(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIOpenInbox(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIOpenOutbox(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIOpenSentItems(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIOpenDeletedItems(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIOpenDrafts(IntPtr pMAPI);
[DllImport("ReadSMS.dll")]
protected static extern bool MAPIGetNextMessage(IntPtr pMAPI, out IntPtr pMessage, bool bUnreadOnly);
}
接下來封裝一個(gè)短信對象
public class SmsMessage:IDisposable
{
public enum RecipientType { UNKNOWN, TO, CC, BCC };
protected IntPtr pMessage;
public SmsMessage()
{
pMessage = IntPtr.Zero;
}
public SmsMessage(IntPtr pMessage)
{
this.pMessage = pMessage;
}
~SmsMessage()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (pMessage != IntPtr.Zero)
{
MessageClose(pMessage);
pMessage = IntPtr.Zero;
}
}
public IntPtr MessagePointer { get { return pMessage; } }
public void GetSenderName(StringBuilder strSenderName)
{
MessageGetSenderName(pMessage, strSenderName, strSenderName.Capacity);
}
public void GetSenderEmail(StringBuilder strSenderEmail)
{
MessageGetSenderEmail(pMessage, strSenderEmail, strSenderEmail.Capacity);
}
public void GetSubject(StringBuilder strSubject)
{
MessageGetSubject(pMessage, strSubject, strSubject.Capacity);
}
/// <summary>
/// Gets the received time
/// </summary>
/// <param name="dt">DateTime received</param>
/// <returns>true on success</returns>
public bool GetReceivedTime(out DateTime dt)
{
int nYear, nMonth, nDay, nHour, nMinute, nSecond;
bool bResult = MessageGetReceivedTime(pMessage, out nYear, out nMonth, out nDay, out nHour, out nMinute, out nSecond);
dt = new DateTime(nYear, nMonth, nDay, nHour, nMinute, nSecond);
return bResult;
}
/// <summary>
/// Gets the received time using the default format (MM/dd/yyyy hh:mm:ss tt)
/// </summary>
/// <param name="strReceivedTime">buffer to receive</param>
/// <returns>true on success</returns>
public bool GetReceivedTime(StringBuilder strReceivedTime)
{
return MessageGetReceivedTimeString(pMessage, strReceivedTime, strReceivedTime.Capacity, "");
}
/// <summary>
/// Gets the received time
/// </summary>
/// <param name="strReceivedTime">buffer to receive</param>
/// <param name="strFormat">format string for date (empty for default)</param>
/// <returns>true on success</returns>
public bool GetReceivedTime(StringBuilder strReceivedTime, string strFormat)
{
return MessageGetReceivedTimeString(pMessage, strReceivedTime, strReceivedTime.Capacity, strFormat);
}
/// <summary>
/// Gets the submit time
/// </summary>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -