从PC机到终端可以用Microsoft ActiveSync 这个同步软件直接文件同步就可以了。。
如下代码,在PC机上 实现 PC机 终端 相互传文件,就引用rapi.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string configFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\";
#region 声明要引用的DLL
// 声明要引用的API
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiUninit();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiGetError();
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiInit();
// 声明要引用的API
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeCloseHandle(IntPtr hObject);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeWriteFile(IntPtr hFile, byte[] lpBuffer,
int nNumberOfbytesToWrite, ref int lpNumberOfbytesWritten, int lpOverlapped);
[DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr CeCreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeCopyFile(string lpExistingFileName, string lpNewFileName, int bFailIfExists);
[DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int CeGetSystemInfo(out SYSTEM_INFO pSI);
[DllImport("rapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool CeGetVersionEx(out OSVERSIONINFO lpVersionInformation);
[DllImport("rapi.dll ", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int CeReadFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfbytesToRead, ref int lpNumberOfbytesRead, int lpOverlapped);
#endregion
#region 声明变量
private const uint GENERIC_WRITE = 0x40000000; // 设置读写权限
private const short CREATE_NEW = 1; // 创建新文件
private const short FILE_ATTRIBUTE_NORMAL = 0x80; // 设置文件属性
private const short INVALID_HANDLE_VALUE = -1; // 错误句柄
private const short CREATE_ALWAYS = 2;
private const uint GENERIC_READ = 0x80000000;
private const short OPEN_EXISTING = 3;
IntPtr remoteFile = IntPtr.Zero; // 本地计算机文件名
String LocalFileName = @"F:\InsertRecordData.txt"; // 远程设备文件名
String RemoteFileName = @"\Application Data\Log\InsertRecordData.txt";
byte[] buffer = new byte[0x1000]; // 传输缓冲区定义为4k
FileStream localFile;
int bytesread = 0;
int byteswritten = 0;
int filepos = 0;
#endregion
#region Rapi初始化
public int RapiInit()
{
int ret = CeRapiInit();
if (ret != 0)
{
// 连接失败,获取失败代码
int e = CeRapiGetError();
// 抛出异常
Marshal.ThrowExceptionForHR(ret);
}
return ret;
}
#endregion
#region 將遠程設備上的文件複製到PCs文件目錄下
/// <summary>
/// 將遠程設備上的文件複製到PCs文件目錄下
/// </summary>
/// <param name="LocalFileName"></param>
/// <param name="RemoteFileName"></param>
/// <param name="Overwrite"></param>
public void CopyFileFromDevice(string LocalFileName, string RemoteFileName, bool Overwrite)
{
FileStream localFile;
IntPtr remoteFile = IntPtr.Zero;
int bytesread = 0;
int create = Overwrite ? CREATE_ALWAYS : CREATE_NEW;
byte[] buffer = new byte[0x1000]; // 4k transfer buffer
// open the remote (device) file
remoteFile = CeCreateFile(RemoteFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// check for success
if ((int)remoteFile == INVALID_HANDLE_VALUE)
{
throw new Exception("不能打开远程设备上指定文件!");
//throw new Exception("不能打開遠程設備上的Material_Tx.xml文件 ");
}
// create the local file
//localFile = new FileStream(LocalFileName, Overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.Write);
localFile = new FileStream(LocalFileName, Overwrite ? FileMode.Create : FileMode.Create, FileAccess.Write);
// read data from remote file into buffer
CeReadFile(remoteFile, buffer, 0x1000, ref bytesread, 0);
while (bytesread > 0)
{
// write it into local file
localFile.Write(buffer, 0, bytesread);
// get more data
if (!Convert.ToBoolean(CeReadFile(remoteFile, buffer, 0x1000, ref bytesread, 0)))
{
CeCloseHandle(remoteFile);
localFile.Close();
throw new Exception("读取远程设备上指定文件失败!");
//throw new Exception("讀取遠程設備上的Material_Tx.xml文件失敗! ");
}
}
// close the remote file
CeCloseHandle(remoteFile);
localFile.Flush();
// close the local file
localFile.Close();
}
#endregion
#region PC文件写入到Wince
/// <summary>
/// PC文件写入到Wince
/// </summary>
public void RapiFile()
{
// 创建远程文件
remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, CREATE_NEW,
FILE_ATTRIBUTE_NORMAL, 0);
// 检查文件是否创建成功
if ((int)remoteFile == INVALID_HANDLE_VALUE)
{
throw new Exception("Could not create remote file");
}
// 打开本地文件
localFile = new FileStream(LocalFileName, FileMode.Open);
// 读取4K字节
bytesread = localFile.Read(buffer, filepos, buffer.Length);
while (bytesread > 0)
{
// 移动文件指针到已读取的位置
filepos += bytesread;
// 写缓冲区数据到远程设备文件
if (!Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread,
ref byteswritten, 0)))
{ // 检查是否成功,不成功关闭文件句柄,抛出异常
CeCloseHandle(remoteFile);
throw new Exception("Could not write to remote file");
}
try
{
// 重新填充本地缓冲区
bytesread = localFile.Read(buffer, 0, buffer.Length);
}
catch (Exception)
{
bytesread = 0;
}
}
// 关闭本地文件
localFile.Close();
// 关闭远程文件
CeCloseHandle(remoteFile);
}
#endregion
#region 参数函数
///
/// OSVERSIONINFO platform type
///
public enum PlatformType : int
{
///
/// Win32 on Windows CE.
///
VER_PLATFORM_WIN32_CE = 3
}
///
/// 操作系统版本信息
///
public struct OSVERSIONINFO
{
internal int dwOSVersionInfoSize;
///
/// 主版本信息
///
public int dwMajorVersion;
///
/// 副版本信息
///
public int dwMinorVersion;
///
/// 编译信息
///
public int dwBuildNumber;
///
/// 操作系统类型
///
public PlatformType dwPlatformId;
}
public struct SYSTEM_INFO
{
/// <summary>
/// Processor architecture
/// </summary>
public ProcessorArchitecture wProcessorArchitecture;
internal ushort wReserved;
/// <summary>
/// Specifies the page size and the granularity of page protection and commitment.
/// </summary>
public int dwPageSize;
/// <summary>
/// Pointer to the lowest memory address accessible to applications and dynamic-link libraries (DLLs).
/// </summary>
public int lpMinimumApplicationAddress;
/// <summary>
/// Pointer to the highest memory address accessible to applications and DLLs.
/// </summary>
public int lpMaximumApplicationAddress;
/// <summary>
/// Specifies a mask representing the set of processors configured into the system. Bit 0 is processor 0; bit 31 is processor 31.
/// </summary>
public int dwActiveProcessorMask;
/// <summary>
/// Specifies the number of processors in the system.
/// </summary>
public int dwNumberOfProcessors;
/// <summary>
/// Specifies the type of processor in the system.
/// </summary>
public ProcessorType dwProcessorType;
/// <summary>
/// Specifies the granularity with which virtual memory is allocated.
/// </summary>
public int dwAllocationGranularity;
/// <summary>
/// Specifies the system抯 architecture-dependent processor level.
/// </summary>
public short wProcessorLevel;
/// <summary>
/// Specifies an architecture-dependent processor revision.
/// </summary>
public short wProcessorRevision;
}
public enum ProcessorArchitecture : short
{
/// <summary>
/// Intel
/// </summary>
Intel = 0,
/// <summary>
/// MIPS
/// </summary>
MIPS = 1,
/// <summary>
/// Alpha
/// </summary>
Alpha = 2,
/// <summary>
/// PowerPC
/// </summary>
PPC = 3,
/// <summary>
/// Hitachi SHx
/// </summary>
SHX = 4,
/// <summary>
/// ARM
/// </summary>
ARM = 5,
/// <summary>
/// IA64
/// </summary>
IA64 = 6,
/// <summary>
/// Alpha 64
/// </summary>
Alpha64 = 7,
/// <summary>
/// Unknown
/// </summary>
Unknown = -1
}
public enum ProcessorType : int
{
/// <summary>
/// 386
/// </summary>
PROCESSOR_INTEL_386 = 386,
/// <summary>
/// 486
/// </summary>
PROCESSOR_INTEL_486 = 486,
/// <summary>
/// Pentium
/// </summary>
PROCESSOR_INTEL_PENTIUM = 586,
/// <summary>
/// P2
/// </summary>
PROCESSOR_INTEL_PENTIUMII = 686,
/// <summary>
/// IA 64
/// </summary>
PROCESSOR_INTEL_IA64 = 2200,
/// <summary>
/// MIPS 4000 series
/// </summary>
PROCESSOR_MIPS_R4000 = 4000,
/// <summary>
/// Alpha 21064
/// </summary>
PROCESSOR_ALPHA_21064 = 21064,
/// <summary>
/// PowerPC 403
/// </summary>
PROCESSOR_PPC_403 = 403,
/// <summary>
/// PowerPC 601
/// </summary>
PROCESSOR_PPC_601 = 601,
/// <summary>
/// PowerPC 603
/// </summary>
PROCESSOR_PPC_603 = 603,
/// <summary>
/// PowerPC 604
/// </summary>
PROCESSOR_PPC_604 = 604,
/// <summary>
/// PowerPC 620
/// </summary>
PROCESSOR_PPC_620 = 620,
/// <summary>
/// Hitachi SH3
/// </summary>
PROCESSOR_HITACHI_SH3 = 10003,
/// <summary>
/// Hitachi SH3E
/// </summary>
PROCESSOR_HITACHI_SH3E = 10004,
/// <summary>
/// Hitachi SH4
/// </summary>
PROCESSOR_HITACHI_SH4 = 10005,
/// <summary>
/// Motorola 821
/// </summary>
PROCESSOR_MOTOROLA_821 = 821,
/// <summary>
/// Hitachi SH3
/// </summary>
PROCESSOR_SHx_SH3 = 103,
/// <summary>
/// Hitachi SH4
/// </summary>
PROCESSOR_SHx_SH4 = 104,
/// <summary>
/// Intel StrongARM
/// </summary>
PROCESSOR_STRONGARM = 2577,//---98x
/// <summary>
/// ARM720
/// </summary>
PROCESSOR_ARM720 = 1824,
/// <summary>
/// ARM820
/// </summary>
PROCESSOR_ARM820 = 2080,
/// <summary>
/// ARM920
/// </summary>
PROCESSOR_ARM920 = 2336,
/// <summary>
/// ARM 7
/// </summary>
PROCESSOR_ARM_7TDMI = 70001
}
#endregion
#region 连接
private void button1_Click(object sender, EventArgs e)
{
if (RapiInit() == 0)
{
MessageBox.Show("连接成功!");
//labelstate.Text = "连接成功";
//btnConnect.Enabled = false;
}
else
{
MessageBox.Show("连接失败!");
}
}
#endregion
#region 上传到PC机
private void button2_Click(object sender, EventArgs e)
{
string localpath = @"F:\InsertRecordData.txt";
CopyFileFromDevice(localpath, @"\Application Data\Log\InsertRecordData.txt", false);
MessageBox.Show("上传成功!", "提示");
}
#endregion
#region 下载到Wince
private void button3_Click(object sender, EventArgs e)
{
RapiFile();
MessageBox.Show("下载成功!", "提示");
}
#endregion
}
}