Remote API(RAPI)之 文件管理

RAPI库由一组函数组成,这些函数可用于通过桌面应用程序管理设备,包括设备的目录文件、设备的注册表和系统信息。

RAPI提供了一组文件管理方法

CeCopyFile:复制文件

CeCreateDirectory:创建目录

CeCreateFile:创建,打开文件、管道、通讯资源、磁盘设备或者控制台。返回一个句柄用来访问对象。

CeDeleteFile:删除文件

CeFindAllFiles:从指定的Windows CE目录中获取所有文件和目录的信息,并且复制到一个包含CE_FIND_DATA结构的数组中

CeFindFirstFile:在目录中查找匹配的给定文件名的一个文件

CeFindClose:关闭指定的查找句柄,CeFindFirstFile和CeFindNextFile函数用这个句柄查找文件

CeFindNextFile:从上一次访问的CeFindFirstFile继续查找文件

CeGetFileAttributes:返回指定文件和目录的属性

CeGetFileSize:获取指定文件的字节大小

CeGetFileTime:获取文件创建日期时间,最后访问日期时间和最后修改日期时间

CeMoveFile:移动(重命名)一个文件或目录

CeReadFile:从文件指针处读取文件

CeWriteFile:从文件指针处写入文件数据

任何RAPI操作都需要首先初始化和设备的连接

CeRapiInit():同步初始化设备

CeRapiInitEx():异步初始化设备,并返回一个事件句柄

操作完毕后在合适的时候断开RAPI连接

CeRapiUninit(): 断开或停止设备连接

下面是复制文件到PDA的一个例子:

        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 int TimeOut = 2000;//异步连接设备超时时间2秒 

        private void btnCopyFileToPDA_Click(object sender, EventArgs e)
        {
            //1、初始化设备连接

            ///---同步连接失败---
            //int ret = CeRapiInit(); 
            //if (ret != 0)
            //{
            //    //连接失败,获取失败代码
            //    int error = CeRapiGetError();
            //    //抛出异常
            //    Marshal.ThrowExceptionForHR(ret);
            //}
           
            ///---采用异步连接---
            Rapiinit ri = new Rapiinit();
            ri.cbsize = Marshal.SizeOf(ri);
            uint hRes = CeRapiInitEx(ref ri);
            ManualResetEvent me = new ManualResetEvent(false);
            me.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(ri.heRapiInit, false);
            if (!me.WaitOne(TimeOut, true))
            {
                CeRapiUninit();
            }

            //2、PC复制文件到PDA

            IntPtr remoteFile = IntPtr.Zero;
            String LocalFileName = @"D:	est.txt";//本地计算机文件名
            String RemoteFileName = @"ResidentFlashFire	est.txt";//远程设备文件名
            byte[] buffer = new byte[0x1000];//传输缓冲区定义为4k
            FileStream localFile;

            int bytesread = 0;
            int bytesrwritten = 0;
            int filepos = 0;

            //创建远程文件
            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.");
            }
            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 bytesrwritten, 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);

            //3、断开连接 文件句柄使用后一定要释放

            CeRapiUninit();

        }


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

        [DllImport("rapi.dll")]
        private static extern uint CeRapiInitEx(ref Rapiinit pRapiInt);

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

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

        [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 dwDesireAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);

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

MSDN:https://msdn.microsoft.com/zh-cn/ee497185

原文地址:https://www.cnblogs.com/Sukie-s-home/p/5780853.html