C#怎么做到PDA与pc数据同步

pda系统为mobile6,怎么才能做到通过网络实现把pc端文件保存到pda上实现数据同步(不是通过usb线同步)?pc机ip已知、文件存放路径已知
您好,首先谢谢您的回答。我是刚开始做mobile开发。现在需要实现这个同步功能,可能是我描述的不太清楚。不想通过同步软件连接usb才能同步,pda用户或许会拿到很远的地方,我就是想让它网络同步,刚做这个没有一点思路,如果有思路也可以说下。谢谢

第一步:创建设备代理应用程序
创建Windows Mobile 5.0 Smartphone SDK平台的智能设备程序,
添加Microsoft.Smartdevice.DeviceAgentTransport的引用:
C:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\Lib。单击 Microsoft.Smartdevice.DeviceAgentTransport.dll,

修改 Program.cs 文件:
using Microsoft.SmartDevice.DeviceAgentTransport;
在 Main 方法内部。
string[] serviceids = {"A92866CA-AE83-4848-9438-501D8DB3CF25"};

IDeviceAgentTransport transport = DeviceAgentTransportFactory.GetAgentTransport();

// Don't keep it waiting.
transport.AcknowledgeLaunch(1, serviceids);

IDevicePacketStream packetstream;

transport.AcceptConnectionEx(serviceids[0], out packetstream);
IPacket packet;
packet = PacketFactory.GetNewPacket();
packet.WriteInt32(Environment.Version.Major);
packet.WriteInt32(Environment.Version.Minor);
packet.WriteInt32(Environment.Version.Build);
packet.WriteInt32(Environment.Version.Revision);
//这里修改成你要发送到PC的内容就好
packetstream.Write(packet);

上述代码将创建一个 IDeviceAgentTransport 对象,然后接受来自台式计算机的连接。该程序会向台式计算机发送一个包含 .NET Compact Framework 版本信息的数据包。
IDeviceAgentTransport: 用于与台式计算机上的 RemoteAgent 进行通信。
IDevicePacketStream: 通过读写实现 IPacket 的对象,与台式计算机上已连接的 DevicePacketStream 交换数据。
IPacket: 保存数据并由实现 IDevicePacketStream 的对象用于在设备代理应用程序和台式计算机之间传输数据。ReadBool 从对象中读取一个布尔值,并使内部迭代器指向数据包中的下一个数据对象。WriteInt32方法是给PC传数据,方法ReadByte;ReadInt32;;ReadString等是从PC读数据

第二步:在数据存储中创建加载项包
启动记事本。
将下面的代码复制到记事本文件中:
<?xml version="1.0" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<ADDONCONTAINER>
<ADDON>
<PACKAGECONTAINER>
<PACKAGE ID="" NAME="DeviceAgent">
<PROPERTYCONTAINER />
<PACKAGETYPECONTAINER>
<PACKAGETYPE Name="ARMV4I" ID="ARMV4I" Protected="True">
<PROPERTYCONTAINER>
<PROPERTY ID="RemotePath" Protected="True">%CSIDL_PROGRAM_FILES%\DeviceAgent</PROPERTY>
<PROPERTY ID="RootPath" Protected="True">C:\DataExchange\DeviceAgent\bin\Debug</PROPERTY>
<PROPERTY ID="CPU" Protected="True">ARMV4I</PROPERTY>
<PROPERTY ID="Host" Protected="True">DeviceAgent</PROPERTY>
</PROPERTYCONTAINER>
<FILECONTAINER>
<FILE ID="DeviceAgent.exe" />
<FILE ID="Microsoft.Smartdevice.DeviceAgentTransport.dll" />
<FILE ID="DeviceAgentTransport.dll" />
</FILECONTAINER>
</PACKAGETYPE>
</PACKAGETYPECONTAINER>
</PACKAGE>
</PACKAGECONTAINER>
</ADDON>
</ADDONCONTAINER>
</xsl:template>
</xsl:stylesheet>

在 Visual Studio 中的“工具”菜单上单击“创建 GUID”。
将出现“创建 GUID”对话框。
选择“注册表格式”,单击“复制”,再单击“退出”。
将 GUID 粘贴到记事本内包标记的 ID 属性中,同时移除括在 GUID 两侧的大括号。
例如,<Package ID="9C50B38D-4259-40b3-AE9F-C5887DD898FF" Name="DeviceAgent">。
将 <PROPERTY ID="RootPath" Protected="True"> 标记的值更改为 DeviceAgent.exe 所在的文件夹。
例如,<PROPERTY ID="RootPath" protected="True">c:\DataExchange\DeviceAgent\bin\debug</PROPERTY>。
注意:
若要确定 DeviceAgent.exe 的位置,请在“解决方案资源管理器”中右击“DeviceAgent”项目,再单击“属性”。单击“生成”选项卡,然后单击“输出路径”旁边的“浏览”。
将 驱动器:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\wce400\armv4i\DeviceAgentTransport.dll 库复制到根路径下指定的路径下。
注意:
此步骤十分重要,原因是 Microsoft.Smartdevice.DeviceAgentTransport.dll 程序集要使用 DeviceAgentTransport.dll 库。这两个文件必须随我们的应用程序一起部署。
在记事本中,保存文件。
\ProgramData\Microsoft\corecon\1.0\addons\package.xsl (Windows Vista)
\Documents and Settings\All Users\Application Data\Microsoft\corecon\1.0\addons\package.xsl(其他)
注意:
如果文件名 package.xsl 已在使用中,请使用其他名称。文件名并不重要,因为该文件夹中的所有文件都将被作为加载项包处理。
退出并重新启动 Visual Studio。
Visual Studio 在启动时将加载该加载项包文件夹。如果 Visual Studio 能正常启动,则说明该包没有语法错误。
数据存储现在包含一个名为 DeviceAgent 的加载项包以及一个由 Visual Studio 唯一生成的 ID。部署该包时,智能设备连接 API 会将三个文件从 DeviceAgent 项目的输出文件夹复制到设备的 驱动器:\Program File\DeviceAgent 目录下,然后在设备上执行 DeviceAgent.exe。

第三步: 创建部署包并与设备代理进行通信的台式机应用程序

创建PC端的Windows应用程序,
添加Microsoft.Smartdevice.Connectivity的引用:
C:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Bin\ Microsoft.Smartdevice.Connectivity.dll

using Microsoft.SmartDevice.Connectivity;
using System.Collections.ObjectModel;

下面的代码可以放到Main中,也可以放到个按钮事件中,不过建议你最好开线程.
// Change the locale ID to correspond to your installation of Visual Studio.
DatastoreManager dsmgr = new DatastoreManager(1033); // 1033是英语,
Platform platform = GetPlatformByName("Windows Mobile 5.0 Smartphone SDK", dsmgr);
Device emulator = platform.GetDevice(platform.GetDefaultDeviceId());
// 这里必须要修改,设备名称,如果你知道平台ID,直接 platform =dsmgr.GetPlatform(platformId)就好,
// 如果platform.GetDefaultDeviceId()找到的不是你的设备,还要和GetPlatformByName()一样,要遍历platform.GetDevices找你的设备,或者你明确知道设备ID
emulator.Connect();
RemoteAgent ra = emulator.GetRemoteAgent(new ObjectId("")); /// 引号里边填入你生成的GUID
ra.Start("command line argument");
DevicePacketStream ps = ra.CreatePacketStream(new ObjectId("A92866CA-AE83-4848-9438-501D8DB3CF25"));
Packet packet;
packet = new Packet();
while (ps.IsConnected())
{
if (ps.IsPacketAvailable())
{
packet = ps.Read();
while (!packet.IsEndOfPacket())
{
switch (packet.ReadDataType())
{
case DataType.Int32Type:
Console.WriteLine("Int32Type: " + packet.ReadInt32().ToString());
break;
case DataType.StringType:
Console.WriteLine("String: " + packet.ReadString());
break;
default:
break;
}
}
break;
}
}
Console.Read();

添加GetPlatformByName方法
private static Platform GetPlatformByName(string p, DatastoreManager dsmgr)
{
// Get all platforms in the datastore.
Collection<Platform> platforms = dsmgr.GetPlatforms();

// Find the platform whose name matches the parameter.
foreach (Platform platform in platforms)
{
if (platform.Name == p) return platform;
}
return null;
}

参考:
智能设备连接应用程序的典型体系结构
若要了解智能设备连接 API,最佳方式是完整地运行使用智能设备连接 API 的典型桌面应用程序。

为了使用 Microsoft.SmartDevice.Connectivity 命名空间,台式机应用程序会添加对 驱动器:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Bin\Microsoft.Smartdevice.Connectivity.dll 的引用。

该应用程序将创建 DatastoreManager 并将其用于枚举 Platform 对象的集合。每个 Platform 都表示台式计算机数据存储中安装的一个平台,例如 Windows Mobile 5.0 Pocket PC。

该应用程序将使用 Platform 枚举 Device 对象的集合。每个 Device 都表示一个属于该平台的设备或仿真程序,例如 Windows Mobile 5.0 Pocket PC 仿真程序。

该应用程序将使用 Device 访问有关设备的信息,并在配置设备后连接至设备。

在连接至设备后,该应用程序可以使用 FileDeployer 将文件部署到设备,或使用 RemoteProcess 启动或停止设备上的进程。

如果应用程序必须执行 FileDeployer、RemoteProcess 或 Device 中未提供的自定义任务,它可以创建一个 RemoteAgent 来将设备代理部署到设备并与之交换数据。设备代理是用户创作的一种智能设备应用程序,它使用 Microsoft.SmartDevice.DeviceAgentTransport 命名空间(针对 Visual C# 或 Visual Basic 设备代理)或 DeviceAgentTransport.dll 库(针对 Visual C++ 设备代理)。若要通过 RemoteAgent 部署设备代理,必须在台式计算机的数据存储中将设备代理注册为加载项包。有关更多信息,请参见加载项包概述。为部署设备代理并与之进行通信,应用程序将执行下列步骤:

台式机应用程序调用 RemoteAgent..::.Start 部署并启动设备代理。

当设备代理在设备上运行后,它会调用 IDeviceAgentTransport..::.AcknowledgeLaunch 确认连接。

设备代理调用 IDeviceAgentTransport..::.AcceptConnectionEx 接受来自台式机的数据包流请求。

台式机应用程序调用 RemoteAgent..::.CreatePacketStream 获取用于数据传输的 DevicePacketStream。

此后,远程应用程序和设备代理会通过将 Packet 或 IPacket 对象读写到各自的设备数据包流中来交换数据。

由于设备代理在设备上运行,因此它具有完全的编程访问权限来收集信息和操作设备。

注意:
本主题前面介绍的设备代理使用 Microsoft.SmartDevice.DeviceAgentTransport 和 .NET Compact Framework。另外,您也可以使用非托管设备端的智能设备连接 API 创建非托管的设备代理。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-11-08
第一步:创建设备代理应用程序
创建Windows Mobile 5.0 Smartphone SDK平台的智能设备程序,
添加Microsoft.Smartdevice.DeviceAgentTransport的引用:
C:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\Lib。单击 Microsoft.Smartdevice.DeviceAgentTransport.dll,

修改 Program.cs 文件:
using Microsoft.SmartDevice.DeviceAgentTransport;
在 Main 方法内部。
string[] serviceids = {"A92866CA-AE83-4848-9438-501D8DB3CF25"};

IDeviceAgentTransport transport = DeviceAgentTransportFactory.GetAgentTransport();

// Don't keep it waiting.
transport.AcknowledgeLaunch(1, serviceids);

IDevicePacketStream packetstream;

transport.AcceptConnectionEx(serviceids[0], out packetstream);
IPacket packet;
packet = PacketFactory.GetNewPacket();
packet.WriteInt32(Environment.Version.Major);
packet.WriteInt32(Environment.Version.Minor);
packet.WriteInt32(Environment.Version.Build);
packet.WriteInt32(Environment.Version.Revision);
//这里修改成你要发送到PC的内容就好
packetstream.Write(packet);

上述代码将创建一个 IDeviceAgentTransport 对象,然后接受来自台式计算机的连接。该程序会向台式计算机发送一个包含 .NET Compact Framework 版本信息的数据包。
IDeviceAgentTransport: 用于与台式计算机上的 RemoteAgent 进行通信。
IDevicePacketStream: 通过读写实现 IPacket 的对象,与台式计算机上已连接的 DevicePacketStream 交换数据。
IPacket: 保存数据并由实现 IDevicePacketStream 的对象用于在设备代理应用程序和台式计算机之间传输数据。ReadBool 从对象中读取一个布尔值,并使内部迭代器指向数据包中的下一个数据对象。WriteInt32方法是给PC传数据,方法ReadByte;ReadInt32;;ReadString等是从PC读数据

第二步:在数据存储中创建加载项包
启动记事本。
将下面的代码复制到记事本文件中:
<?xml version="1.0" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<ADDONCONTAINER>
<ADDON>
<PACKAGECONTAINER>
<PACKAGE ID="" NAME="DeviceAgent">
<PROPERTYCONTAINER />
<PACKAGETYPECONTAINER>
<PACKAGETYPE Name="ARMV4I" ID="ARMV4I" Protected="True">
<PROPERTYCONTAINER>
<PROPERTY ID="RemotePath" Protected="True">%CSIDL_PROGRAM_FILES%\DeviceAgent</PROPERTY>
<PROPERTY ID="RootPath" Protected="True">C:\DataExchange\DeviceAgent\bin\Debug</PROPERTY>
<PROPERTY ID="CPU" Protected="True">ARMV4I</PROPERTY>
<PROPERTY ID="Host" Protected="True">DeviceAgent</PROPERTY>
</PROPERTYCONTAINER>
<FILECONTAINER>
<FILE ID="DeviceAgent.exe" />
<FILE ID="Microsoft.Smartdevice.DeviceAgentTransport.dll" />
<FILE ID="DeviceAgentTransport.dll" />
</FILECONTAINER>
</PACKAGETYPE>
</PACKAGETYPECONTAINER>
</PACKAGE>
</PACKAGECONTAINER>
</ADDON>
</ADDONCONTAINER>
</xsl:template>
</xsl:stylesheet>

在 Visual Studio 中的“工具”菜单上单击“创建 GUID”。
将出现“创建 GUID”对话框。
选择“注册表格式”,单击“复制”,再单击“退出”。
将 GUID 粘贴到记事本内包标记的 ID 属性中,同时移除括在 GUID 两侧的大括号。
例如,<Package ID="9C50B38D-4259-40b3-AE9F-C5887DD898FF" Name="DeviceAgent">。
将 <PROPERTY ID="RootPath" Protected="True"> 标记的值更改为 DeviceAgent.exe 所在的文件夹。
例如,<PROPERTY ID="RootPath" protected="True">c:\DataExchange\DeviceAgent\bin\debug</PROPERTY>。
注意:
若要确定 DeviceAgent.exe 的位置,请在“解决方案资源管理器”中右击“DeviceAgent”项目,再单击“属性”。单击“生成”选项卡,然后单击“输出路径”旁边的“浏览”。
将 驱动器:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\wce400\armv4i\DeviceAgentTransport.dll 库复制到根路径下指定的路径下。
注意:
此步骤十分重要,原因是 Microsoft.Smartdevice.DeviceAgentTransport.dll 程序集要使用 DeviceAgentTransport.dll 库。这两个文件必须随我们的应用程序一起部署。
在记事本中,保存文件。
\ProgramData\Microsoft\corecon\1.0\addons\package.xsl (Windows Vista)
\Documents and Settings\All Users\Application Data\Microsoft\corecon\1.0\addons\package.xsl(其他)
注意:
如果文件名 package.xsl 已在使用中,请使用其他名称。文件名并不重要,因为该文件夹中的所有文件都将被作为加载项包处理。
退出并重新启动 Visual Studio。
Visual Studio 在启动时将加载该加载项包文件夹。如果 Visual Studio 能正常启动,则说明该包没有语法错误。
数据存储现在包含一个名为 DeviceAgent 的加载项包以及一个由 Visual Studio 唯一生成的 ID。部署该包时,智能设备连接 API 会将三个文件从 DeviceAgent 项目的输出文件夹复制到设备的 驱动器:\Program File\DeviceAgent 目录下,然后在设备上执行 DeviceAgent.exe。

第三步: 创建部署包并与设备代理进行通信的台式机应用程序

创建PC端的Windows应用程序,
添加Microsoft.Smartdevice.Connectivity的引用:
C:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Bin\ Microsoft.Smartdevice.Connectivity.dll

using Microsoft.SmartDevice.Connectivity;
using System.Collections.ObjectModel;

下面的代码可以放到Main中,也可以放到个按钮事件中,不过建议你最好开线程.
// Change the locale ID to correspond to your installation of Visual Studio.
DatastoreManager dsmgr = new DatastoreManager(1033); // 1033是英语,
Platform platform = GetPlatformByName("Windows Mobile 5.0 Smartphone SDK", dsmgr);
Device emulator = platform.GetDevice(platform.GetDefaultDeviceId());
// 这里必须要修改,设备名称,如果你知道平台ID,直接 platform =dsmgr.GetPlatform(platformId)就好,
// 如果platform.GetDefaultDeviceId()找到的不是你的设备,还要和GetPlatformByName()一样,要遍历platform.GetDevices找你的设备,或者你明确知道设备ID
emulator.Connect();
RemoteAgent ra = emulator.GetRemoteAgent(new ObjectId("")); /// 引号里边填入你生成的GUID
ra.Start("command line argument");
DevicePacketStream ps = ra.CreatePacketStream(new ObjectId("A92866CA-AE83-4848-9438-501D8DB3CF25"));
Packet packet;
packet = new Packet();
while (ps.IsConnected())
{
if (ps.IsPacketAvailable())
{
packet = ps.Read();
while (!packet.IsEndOfPacket())
{
switch (packet.ReadDataType())
{
case DataType.Int32Type:
Console.WriteLine("Int32Type: " + packet.ReadInt32().ToString());
break;
case DataType.StringType:
Console.WriteLine("String: " + packet.ReadString());
break;
default:
break;
}
}
break;
}
}
Console.Read();

添加GetPlatformByName方法
private static Platform GetPlatformByName(string p, DatastoreManager dsmgr)
{
// Get all platforms in the datastore.
Collection<Platform> platforms = dsmgr.GetPlatforms();

// Find the platform whose name matches the parameter.
foreach (Platform platform in platforms)
{
if (platform.Name == p) return platform;
}
return null;
}

参考:
智能设备连接应用程序的典型体系结构
若要了解智能设备连接 API,最佳方式是完整地运行使用智能设备连接 API 的典型桌面应用程序。

为了使用 Microsoft.SmartDevice.Connectivity 命名空间,台式机应用程序会添加对 驱动器:\Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Bin\Microsoft.Smartdevice.Connectivity.dll 的引用。

该应用程序将创建 DatastoreManager 并将其用于枚举 Platform 对象的集合。每个 Platform 都表示台式计算机数据存储中安装的一个平台,例如 Windows Mobile 5.0 Pocket PC。

该应用程序将使用 Platform 枚举 Device 对象的集合。每个 Device 都表示一个属于该平台的设备或仿真程序,例如 Windows Mobile 5.0 Pocket PC 仿真程序。

该应用程序将使用 Device 访问有关设备的信息,并在配置设备后连接至设备。

在连接至设备后,该应用程序可以使用 FileDeployer 将文件部署到设备,或使用 RemoteProcess 启动或停止设备上的进程。

如果应用程序必须执行 FileDeployer、RemoteProcess 或 Device 中未提供的自定义任务,它可以创建一个 RemoteAgent 来将设备代理部署到设备并与之交换数据。设备代理是用户创作的一种智能设备应用程序,它使用 Microsoft.SmartDevice.DeviceAgentTransport 命名空间(针对 Visual C# 或 Visual Basic 设备代理)或 DeviceAgentTransport.dll 库(针对 Visual C++ 设备代理)。若要通过 RemoteAgent 部署设备代理,必须在台式计算机的数据存储中将设备代理注册为加载项包。有关更多信息,请参见加载项包概述。为部署设备代理并与之进行通信,应用程序将执行下列步骤:

台式机应用程序调用 RemoteAgent..::.Start 部署并启动设备代理。

当设备代理在设备上运行后,它会调用 IDeviceAgentTransport..::.AcknowledgeLaunch 确认连接。

设备代理调用 IDeviceAgentTransport..::.AcceptConnectionEx 接受来自台式机的数据包流请求。

台式机应用程序调用 RemoteAgent..::.CreatePacketStream 获取用于数据传输的 DevicePacketStream。

此后,远程应用程序和设备代理会通过将 Packet 或 IPacket 对象读写到各自的设备数据包流中来交换数据。

由于设备代理在设备上运行,因此它具有完全的编程访问权限来收集信息和操作设备。

注意:
本主题前面介绍的设备代理使用 Microsoft.SmartDevice.DeviceAgentTransport 和 .NET Compact Framework。另外,您也可以使用非托管设备端的智能设备连接 API 创建非托管的设备代理。
第2个回答  2010-12-05
实际里面核心是一样的,都带着操作系统,一般手机采用的是SYMBIAN或者是WINDOWSMOBILE系统,PDA也有系统,同步和手机也是一样的,随着更新换代,WIFI以后肯定要取代现在的蓝牙;PDA可以借助添加移动模块来进行打电话,还有加很多模块来添加很多功能,而手机是集成了这个打电话的功能,智能手机和PC之间同步和PDA基本上局限不大,但是你最好要看和你电脑里经常同步是什么资料,来选择是否买PDA和智能手机
第3个回答  2010-12-06
学习中...
相似回答