/// <summary> /// 将当前目录移动到目标目录 /// </summary> /// <param name="targetDirectory">目标文件夹</param> public void MoveTo(string targetDirectory) { var start = targetDirectory.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1; var length = targetDirectory.Length - start; var name = targetDirectory.Substring(start, length); if (!LocalDisk.IsValidFileName(name)) { throw new ArgumentException("the name '" + name + "' contains invalid characters"); } IDirectory moveToDirectory = new Directory(targetDirectory.Substring(0, targetDirectory.Length - name.Length - 1), disk); moveToDirectory.Create(); DirectoryInfo.MoveTo(targetDirectory); path = targetDirectory; Refresh(); }
/// <summary> /// 移动到指定文件 /// </summary> public void Rename(string newName) { LocalDisk.IsValidFileName(newName); if (newName.Contains(Path.AltDirectorySeparatorChar.ToString())) { throw new ArgumentException("rename can't be used to change a files location use Move(string newPath) instead."); } string newExtension = System.IO.Path.GetExtension(newName); string newFileName = System.IO.Path.GetFileNameWithoutExtension(newName); IFile targetFile = new File(directory + Path.AltDirectorySeparatorChar + newFileName + newExtension, disk); if (targetFile.Exists) { throw new ArgumentException("duplicate file name:" + newName); } FileInfo.MoveTo(directory + Path.AltDirectorySeparatorChar + newFileName + newExtension); fileName = newFileName; extension = newExtension; Refresh(); }
/// <summary> /// 重命名当前文件夹 /// </summary> /// <param name="newName">新的文件夹名字</param> public void Rename(string newName) { if (string.IsNullOrEmpty(newName)) { throw new ArgumentNullException("you can't send a empty or null string to rename an asset. trying to rename " + path); } if (!LocalDisk.IsValidFileName(newName)) { throw new ArgumentException("the name '" + newName + "' contains invalid characters"); } if (newName.Contains(System.IO.Path.AltDirectorySeparatorChar.ToString())) { throw new ArgumentException("rename can't be used to change a files location use Move(string newPath) instead."); } var subPath = Path.Substring(0, Path.LastIndexOf(System.IO.Path.AltDirectorySeparatorChar) + 1); var newPath = subPath + newName; DirectoryInfo.MoveTo(newPath); path = newPath; Refresh(); }