复制文件时出现RAPIException

时间:2019-02-13 13:45:50

标签: c# .net rapi

要将文件从Windows 10计算机复制到Windows CE 5.0设备,我使用以下代码(这是OpenNETCF Rapi的一部分,用于记述原始创建者):

public void CopyFileToDevice(string LocalFileName, string RemoteFileName, bool Overwrite)
    {
        // check for connection
        CheckConnection();

        FileStream localFile;
        IntPtr remoteFile = IntPtr.Zero;

        int bytesread = 0;
        int byteswritten = 0;
        int filepos = 0;
        int create = Overwrite ? CREATE_ALWAYS : CREATE_NEW;
        byte[] buffer = new byte[(int)m_FileCopyBufferSize];  // default size 4k transfer buffer

        // create the remote file
        remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, create, FILE_ATTRIBUTE_NORMAL, 0);

        // check for success
        if (remoteFile.ToInt64() == INVALID_HANDLE_VALUE)
        {
            throw new RAPIException("Could not create remote file");
        }

        // open the local file
        localFile = new FileStream(LocalFileName, FileMode.Open, FileAccess.Read);

        // read 4k of data
        bytesread = localFile.Read(buffer, filepos, buffer.Length);
        while (bytesread > 0)
        {
            // move remote file pointer # of bytes read
            filepos += bytesread;


            // write our buffer to the remote file
            if (!Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread, ref byteswritten, 0)))
            { // check for success
                CeCloseHandle(remoteFile);

                throw new RAPIException("Could not write to remote file");
            }
            try
            {
                // refill the local buffer
                bytesread = localFile.Read(buffer, 0, buffer.Length);
            }
            catch (Exception)
            {
                bytesread = 0;
            }
        }

        // close the local file
        localFile.Close();

        // close the remote file
        CeCloseHandle(remoteFile);
    }    

有时此解决方案有效,但在不更改代码,文件或设备的情况下,它突然抛出了RAPIException: "Could not write to remote file". 一段时间后CeWriteFile函数失败会导致这种情况发生。

什么可能导致此错误?

0 个答案:

没有答案