使用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