winCE与本地电脑之间的文件传递

该博客介绍了一个在Windows CE(WinCE)设备与本地电脑之间进行文件传输的实现方法。通过调用rapi.dll中的API函数,实现了文件的上传(CopyFileToPDA)和下载(CopyFileToPC)功能,包括文件的创建、打开、读取、写入和关闭操作。在过程中,使用了手动重置事件(ManualResetEvent)来初始化设备,并进行了错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace pctoce
{
    public partial class Form1 : Form
    {
        // 声明要引用的API
        [DllImport("rapi.dll")]private static extern uint CeRapiUninit();
        [DllImport("rapi.dll")]private static extern uint CeRapiInitEx(ref RAPIINIT pRapiInit);
        [DllImport("rapi.dll", CharSet=CharSet.Unicode)] internal static extern int CeDeleteFile(string FileName);
        [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)] public static extern int CeFindFirstFile(string lpFileName,ref CE_FIND_DATA lpFindFileData);
        [DllImport("rapi.dll", CharSet=CharSet.Unicode)] internal static extern int CeReadFile(int hFile,byte[] lpBuffer,int lpOverlapped,out int laji,int gouride);
        [DllImport("rapi.dll", CharSet=CharSet.Unicode)] internal static extern int CeGetFileSize(int hFile, int lpOverlapped);

        public struct CE_FIND_DATA
        {
        public int dwFileAttributes;
        public FILETIME ftCreationTime;
        public int ftCreationTime2;
        public FILETIME ftLastAccessTime;
        public FILETIME ftLastWriteTime;
        public int nFileSizeHigh;
        public int nFileSizeLow;
        public int dwOID;
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
        public string cFileName;
        }

        [StructLayout(LayoutKind.Explicit)]
        private struct RAPIINIT
        {
        [FieldOffset(0)] public int cbsize;
        [FieldOffset(4)] public System.IntPtr heRapiInit;
        [FieldOffset(8)] public System.IntPtr hrRapiInit;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CopyFileToPDA(@"C:/Documents and Settings/All Users/Documents/My Pictures/示例图片/Blue hills.jpg", @"/Temp/Blue hills.jpg");


        }

 

        private bool InitDevice(int nTimeout)
        {
            RAPIINIT ri = new RAPIINIT();
            ri.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(ri);
            uint hRes = CeRapiInitEx(ref ri);

            ManualResetEvent me = new ManualResetEvent(false);
            me.Handle = ri.heRapiInit;

            if (!me.WaitOne(nTimeout, true))
            {
                CeRapiUninit();
                return false;
            }
            else
            {
                return true;
            }
        }

 


        [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
        internal static extern int CeRapiGetError();
        [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
        internal static extern int CeRapiInit();

        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;
        // 错误句柄
        IntPtr remoteFile = IntPtr.Zero;

        byte[] buffer = new byte[0x1000];
        // 传输缓冲区定义为4k
        FileStream localFile;
        int bytesread = 0;
        int byteswritten = 0;
        int filepos = 0;
       
        // 本地计算机文件名   LocalFileName ; 
        // 远程设备文件名  RemoteFileName;
        public bool CopyFileToPDA(string LocalFileName, string RemoteFileName)
        {
            if (InitDevice(2000))
            {
                //查找远程文件
                CE_FIND_DATA findData = new CE_FIND_DATA();
                int jieg = CeFindFirstFile(RemoteFileName, ref findData);

                if (jieg != -1)
                {
                    CeDeleteFile(RemoteFileName);
                }

                // 创建远程文件
                remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
                // 检查文件是否创建成功
                if ((int)remoteFile == INVALID_HANDLE_VALUE)
                {
                    throw new Exception("创建文件失败!");
                }
                else
                {
                    // 打开本地文件
                    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("写远程文件失败!");
                        }
                        try
                        {
                            // 重新填充本地缓冲区
                            bytesread = localFile.Read(buffer, 0, buffer.Length);
                        }
                        catch (Exception)
                        {
                            bytesread = 0;
                        }
                    }
                    // 关闭本地文件
                    localFile.Close();
                    // 关闭远程文件
                    CeCloseHandle(remoteFile);
                    filepos = 0;
                    CeRapiUninit();
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        // 设置读写权限
        private const short CREATE_OPEN = 3;
        private const uint GENERIC_READ = 0x80000000;


        // 1 成功 0 失败 -1 没找到文件
        public string CopyFileToPC(string RemoteFileName, string LocalFileName)
        {
            if (InitDevice(2000))
            {
                IntPtr fileh = IntPtr.Zero;
                fileh = CeCreateFile(RemoteFileName, GENERIC_READ, 0, 0, CREATE_OPEN, FILE_ATTRIBUTE_NORMAL, 0);
                if ((int)fileh == INVALID_HANDLE_VALUE)
                {
                    return "-1";
                }
                else
                {
                    try
                    {
                        FileStream writer = new FileStream(LocalFileName, FileMode.Create);
                        int size = CeGetFileSize((int)fileh, 0);
                        byte[] buff = new byte[400096];
                        int readed = 400096;
                        while (readed == 400096)
                        {
                            CeReadFile((int)fileh, buff, 400096, out readed, 0);
                            writer.Write(buff, 0, readed);
                        }
                        CeCloseHandle(fileh);
                        writer.Close();
                        return "1";
                    }
                    catch
                    {
                        return "0";
                    }
                    CE_FIND_DATA findData = new CE_FIND_DATA();
                    int jieg = CeFindFirstFile(RemoteFileName, ref findData);

                    if (jieg != -1)
                    {
                        CeDeleteFile(RemoteFileName);
                    }

                }
            }
            else
            {
                return "0";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CopyFileToPC(@"/Temp/6901028310192.jpg", @"c:/6901028310192.jpg");
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值