/// <summary> /// Moves a virtual file to a new directory, optioanlly renaming it in the process. /// </summary> /// <param name="sourceFile">The source file to be moved.</param> /// <param name="targetDirectory">The destination directory.</param> /// <param name="newName">An optional new name. Supply <c>null</c> to use the original name of the source file.</param> public static void MoveFile(VirtualFile sourceFile, UnifiedDirectory targetDirectory, string newName) { if (String.IsNullOrEmpty(newName)) { newName = sourceFile.Name; } string targetVirtualFilePath = VirtualPathUtility.Combine(targetDirectory.VirtualPath, newName); UnifiedFile unifiedSourceFile = sourceFile as UnifiedFile; if (unifiedSourceFile != null) { IVersioningFile versioningFile = sourceFile as IVersioningFile; if (versioningFile != null) { ThrowIfCheckedOut(versioningFile); } if (!IsPathAvailable(targetVirtualFilePath)) { throw new ArgumentException("Moving a file with the same name as a file or a folder in the target directory is not allowed"); } unifiedSourceFile.MoveTo(targetVirtualFilePath); } // Can't move a file unless its a UnifiedFile. VirtualFile does not specify any remove methods. }