使用RAPI。這是一個codeplex項目,爲Rapi.dll和ActiveSync提供託管包裝類。它可以讓桌面.NET應用程序與繫留的移動設備進行通信。包裝起源於OpenNetCF project,但現在單獨管理。
您可以使用整個RAPI項目DLL,因爲它從該項目發運,或者只是使用您需要的代碼的子集。我需要在連接時在設備上創建文件,因此我不需要性能統計信息或Rapi中包含的設備註冊表內容。所以,我只是抓住了我所需要的3個源文件...
它爲我工作的方式,是這樣的:
- 1.用ActiveSync(DccManSink)來檢測移動設備的連接/斷開狀態
- 使用RAPI包裝將文件複製到設備,在設備上創建文件,從設備複製文件等等。
private DccMan DeviceConnectionMgr;
private int AdviceCode;
private int ConnectionStatus = 1;
private System.Threading.AutoResetEvent DeviceConnectionNotification = new System.Threading.AutoResetEvent(false);
public void OnConnectionError()
{
ConnectionStatus = -1;
DeviceConnectionNotification.Set();
}
public void OnIpAssigned(int address)
{
ConnectionStatus = 0;
DeviceConnectionNotification.Set();
}
private void btnCopyToDevice_Click(object sender, EventArgs e)
{
// copy the database (in the form of an XML file) to the connected device
Cursor.Current = Cursors.WaitCursor;
// register for events and wait.
this.DeviceConnectionMgr = new DccMan();
DccManSink deviceEvents = new DccManSink();
deviceEvents.IPChange += new IPAddrHandler(this.OnIpAssigned);
deviceEvents.Error += new ErrorHandler(this.OnConnectionError);
((IDccMan)DeviceConnectionMgr).Advise(deviceEvents, out this.AdviceCode);
// should do this asynchronously, with a timeout; too lazy.
this.statusLabel.Text = "Waiting for a Windows Mobile device to connect....";
this.Update();
Application.DoEvents(); // allow the form to update
bool exitSynchContextBeforeWait = false;
DeviceConnectionNotification.WaitOne(SECONDS_TO_WAIT_FOR_DEVICE * 1000, exitSynchContextBeforeWait);
if (ConnectionStatus == 0)
{
this.statusLabel.Text = "The Device is now connected.";
this.Update();
Application.DoEvents(); // allow the form to update
RAPI deviceConnection = new RAPI();
deviceConnection.Connect(true, 120); // wait up to 2 minutes until connected
if (deviceConnection.Connected)
{
this.statusLabel.Text = "Copying the database file to the connected Windows Mobile device.";
this.Update();
Application.DoEvents(); // allow the form to update
string destPath = "\\Storage Card\\Application Data\\MyApp\\db.xml";
deviceConnection.CopyFileToDevice(sourceFile,
destPath,
true);
this.statusLabel.Text = "Successfully copied the file to the Windows Mobile device....";
}
else
{
this.statusLabel.Text = "Oh, wait, it seems the Windows Mobile device isn't really connected? Sorry.";
}
}
else
{
this.statusLabel.Text = "Could not copy the file because the Device does not seem to be connected.";
}
Cursor.Current = Cursors.Default;
}
聽起來像是正是我需要的!但是,我還不能測試它。由於我只需要從PDA獲取文件,這些文件位於特定的常量目錄中,這看起來可以起作用。 – 2010-03-04 19:18:15
我有麻煩做這項工作。你使用什麼樣的項目?任何不在代碼中的東西我應該知道......? 我特別不能做任何類型的DCCMan或AdviceCode。 謝謝。 – 2010-03-09 19:56:04
Whadaya是什麼意思? 「做任何類型的......」。您需要codeplex.com上RAPI項目的代碼。你有這個,對吧?類似DccManSink和RAPI的類在該代碼中定義。 AdviceCode只是一個int。 (是的,當我從一個工作項目中摘錄這段代碼時,我誤將它遺漏了) – Cheeso 2010-03-09 22:37:41