コード例 #1
0
        /// <summary>
        /// Copies a file to the target folder, optionally with a new file name.
        /// </summary>
        /// <param name="sourceFile">The source file.</param>
        /// <param name="targetDirectory">The target directory.</param>
        /// <param name="newName">The new name. Supply <c>null</c> to use the name of the source file.</param>
        public static void CopyFile(VirtualFile sourceFile, UnifiedDirectory targetDirectory, string newName)
        {
            if (String.IsNullOrEmpty(newName))
            {
                newName = sourceFile.Name;
            }

            string targetVirtualFilePath = VirtualPathUtility.Combine(targetDirectory.VirtualPath, newName);

            targetVirtualFilePath = FindAvailableVirtualPath(targetVirtualFilePath);

            UnifiedFile unifiedSourceFile = sourceFile as UnifiedFile;

            if (unifiedSourceFile != null)
            {
                // If the source file is a UnifiedFile use its copy implementation
                unifiedSourceFile.CopyTo(targetVirtualFilePath);
            }
            else
            {
                // If the source is unknown. i.e. implementing VirtualFile but not UnifiedFile.
                // Use the support methods of UnifiedFile to copy the source VirtualFile.
                UnifiedFile.CopyTo(sourceFile, targetVirtualFilePath);
            }
            //TODO: Implement copying of VersioningFile to a non-versioning provider.
            // The versioning providers overrides the [Copy/Move]To methods of UnifiedFile,
            // but throws an exception if trying to move or copy a file FROM a versioning
            // provider to another provider
        }