Пример #1
0
        /// <summary>
        /// Rename/move a file. For local file system and WebDAV, the
        /// specified file is moved, i.e. the file destination can be
        /// in a different directory/path. In contrast, for FTP the
        /// file is renamed, i.e. its destination must be in the same
        /// directory/path.
        /// </summary>
        /// <param name="iocFrom">Source file path.</param>
        /// <param name="iocTo">Target file path.</param>
        public static void RenameFile(IOConnectionInfo iocFrom, IOConnectionInfo iocTo)
        {
            RaiseIOAccessPreEvent(iocFrom, iocTo, IOAccessType.Move);

#if !ModernKeePassLib
            if (iocFrom.IsLocalFile())
            {
                File.Move(iocFrom.Path, iocTo.Path); return;
            }

#if !KeePassLibSD
            WebRequest req = CreateWebRequest(iocFrom);
            if (req != null)
            {
                string strToCnc = UrlUtil.GetCanonicalUri(iocTo.Path);

                if (IsHttpWebRequest(req))
                {
#if KeePassUAP
                    throw new NotSupportedException();
#else
                    req.Method = "MOVE";
                    req.Headers.Set("Destination", strToCnc);                     // Full URL supported
#endif
                }
                else if (IsFtpWebRequest(req))
                {
#if KeePassUAP
                    throw new NotSupportedException();
#else
                    req.Method = WebRequestMethods.Ftp.Rename;
                    string strToName = UrlUtil.GetFileName(strToCnc);

                    // We're affected by .NET bug 621450:
                    // https://connect.microsoft.com/VisualStudio/feedback/details/621450/problem-renaming-file-on-ftp-server-using-ftpwebrequest-in-net-framework-4-0-vs2010-only
                    // Prepending "./", "%2E/" or "Dummy/../" doesn't work.

                    ((FtpWebRequest)req).RenameTo = strToName;
#endif
                }
                else if (IsFileWebRequest(req))
                {
                    File.Move(UrlUtil.FileUrlToPath(iocFrom.Path),
                              UrlUtil.FileUrlToPath(iocTo.Path));
                    return;
                }
                else
                {
#if KeePassUAP
                    throw new NotSupportedException();
#else
                    req.Method = WrmMoveFile;
                    req.Headers.Set(WrhMoveFileTo, strToCnc);
#endif
                }

#if !KeePassUAP // Unreachable code
                DisposeResponse(req.GetResponse(), true);
#endif
            }
#endif

            // using(Stream sIn = IOConnection.OpenRead(iocFrom))
            // {
            //	using(Stream sOut = IOConnection.OpenWrite(iocTo))
            //	{
            //		MemUtil.CopyStream(sIn, sOut);
            //		sOut.Close();
            //	}
            //
            //	sIn.Close();
            // }
            // DeleteFile(iocFrom);
#endif
        }