public static void CopyFileToDevice(String SourcePath, String DestinationPath, bool Overwrite) { try { if (rapi.DevicePresent) { if (!rapi.Connected) { rapi.Connect(); } try { RAPI.RAPIFileAttributes rattr = rapi.GetDeviceFileAttributes(DestinationPath); if ((rattr & RAPI.RAPIFileAttributes.Directory).Equals(RAPI.RAPIFileAttributes.Directory)) //if destination path exist and it's a directory { FileAttributes attr = File.GetAttributes(SourcePath); if (!(attr & FileAttributes.Directory).Equals(FileAttributes.Directory)) //if source path is not a directory. { DestinationPath = DestinationPath.TrimEnd('\\') + '\\' + Path.GetFileName(SourcePath); //Append the file name after the destination directory } } } catch { //if destination path is a non-existing file, it's a normal situation, do nothing. } rapi.CopyFileToDevice(SourcePath, DestinationPath, Overwrite); if (rapi != null && rapi.Connected) { rapi.Disconnect(); } } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + " ; \r\n" + ex.StackTrace); } }