コード例 #1
0
        /// <summary>
        /// Testing of rapi.CopyFileToDevice revealed that if the connection is broken that a subsequent attempt, without first shutting
        /// down this application, will cause rapi.CopyFileToDevice to fail because of a locking problem with the source file.
        /// This method resolves this problem.  It's also possible that the destination file already exists and is locked.
        ///
        /// Note: Code has been introduced to explicitly set the time of the copied file to be identical to that on the desktop
        ///       but it doesn't seem to work correctly, with the time equivalent to GMT-8.  This must be a bug within RAPI.
        /// </summary>
        /// <param name="rapi"></param>
        /// <param name="srcPath"></param>         // The path on the desktop
        /// <param name="fileName"></param>
        /// <param name="destPath"></param>        // The path on the mobile device
        /// <param name="ExitMsgShown"></param>    // A flag in DataXfer that prevents multiple message boxes from being shown
        /// <returns></returns>
        public static bool CopyFileToDevice(RAPI rapi, string srcPath, string fileName, string destPath, ref bool ExitMsgShown)
        {
            srcPath  = Tools.EnsureFullPath(srcPath);
            destPath = Tools.EnsureFullPath(destPath);
            string tmpPath = SysInfo.Data.Paths.Temp; // {Temp Directory} + AppFilename

            try
            {
                DateTime dateTimeInfo = File.GetLastWriteTime(srcPath + fileName);

                if (CheckSourceFileAccess(srcPath + fileName))
                {
                    // There appear to be no problems copying file directly
                    rapi.CopyFileToDevice(srcPath + fileName, destPath + fileName, true); // Copy file to device, overwriting if necessary
                }
                else // File is definitely locked (to RAPI copy function) so use more complex approach
                {
                    // First see if we can just copy the locked file directly to Temp directory
                    if (!File.Exists(tmpPath + fileName))
                    {
                        File.Copy(srcPath + fileName, tmpPath + fileName);
                        rapi.CopyFileToDevice(tmpPath + fileName, destPath + fileName, true); // Copy file to device, overwriting if necessary
                    }
                    else // This file is locked too so find a free filename we can use
                    {
                        int    sepCharPos = fileName.LastIndexOf(".");
                        string origName   = fileName.Substring(0, sepCharPos);
                        string origExt    = fileName.Substring(sepCharPos);
                        string newName    = "";

                        int i = 1;
                        do
                        {
                            if (!File.Exists(tmpPath + origName + i.ToString() + origExt))
                            {
                                newName = origName + i.ToString() + origExt;
                            }
                            else
                            {
                                i++;
                            }
                        } while (newName == "");

                        File.Copy(srcPath + fileName, tmpPath + newName);

                        if (rapi.DeviceFileExists(destPath + fileName))
                        {
                            rapi.DeleteDeviceFile(destPath + fileName);
                        }

                        rapi.CopyFileToDevice(tmpPath + newName, destPath + fileName, true); // Copy file to device, overwriting if necessary
                    }
                }

                // If there's an error setting the device filetime then handle separately
                try
                {
                    // Note: One would think that these next 2 statements would set the correct time of the file on the mobile device but they don't appear to. :-(
                    //       The datestamp on the device file seems to be in a different time zone.
                    rapi.SetDeviceFileTime(destPath + fileName, RAPI.RAPIFileTime.CreateTime, dateTimeInfo);
                    rapi.SetDeviceFileTime(destPath + fileName, RAPI.RAPIFileTime.LastModifiedTime, dateTimeInfo);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error setting device filetime.  Message: " + e.Message);
                }
            }

            catch (Exception e)
            {
                if (!ExitMsgShown)
                {
                    ExitMsgShown = true;
                    string msg = "Error copying file to mobile device" + "\n" + e.Message;
                    msg += "\n\nSource Path: " + srcPath;
                    msg += "\nDest Path: " + destPath;
                    msg += "\nFilename: " + fileName;
                    msg += "\n\nPlease reconnect and try again!";

                    Debug.WriteLine(msg);
                    Tools.ShowMessage(msg, SysInfo.Data.Admin.AppName);
                }
                return(false);
            }

            return(true);
        }