/// <summary> /// 复制文件夹 /// </summary> /// <param name="targetDirectroy">目标文件夹</param> public IDirectory CopyTo(string targetDirectroy) { LocalDisk.GuardValidatePath(targetDirectroy); IDirectory dir = new Directory(targetDirectroy, disk); dir.Create(); var files = System.IO.Directory.GetFiles(Path); string fileName; string destFile; foreach (var s in files) { fileName = System.IO.Path.GetFileName(s); destFile = targetDirectroy + System.IO.Path.AltDirectorySeparatorChar + fileName; System.IO.File.Copy(s, destFile, true); } IDirectory drinfo; foreach (var info in System.IO.Directory.GetDirectories(Path)) { drinfo = new Directory(info, disk); drinfo.CopyTo(targetDirectroy + System.IO.Path.AltDirectorySeparatorChar + drinfo.Name); } return(((Directory)dir).Refresh()); }
/// <summary> /// 移动到指定文件 /// </summary> public void MoveTo(string targetDirectory) { LocalDisk.ValidatePath(targetDirectory); (new Directory(targetDirectory, disk)).Create(); FileInfo.MoveTo(targetDirectory + Path.AltDirectorySeparatorChar + fileName + extension); directory = targetDirectory; Refresh(); }
/// <summary> /// 子文件夹是否存在 /// </summary> /// <param name="directoryPath">子文件夹的相对路径</param> /// <returns>是否存在</returns> public bool Exists(string directoryPath) { LocalDisk.GuardValidatePath(directoryPath); directoryPath = LocalDisk.NormalizePath(directoryPath); var dir = new DirectoryInfo(path + directoryPath); return(dir.Exists); }
/// <summary> /// 创建子目录文件夹,如果文件夹已经存在那么不会进行任何操作 /// </summary> /// <param name="directoryPath">子目录相对路径</param> /// <returns>子文件夹实例</returns> public IDirectory Create(string directoryPath) { LocalDisk.GuardValidatePath(directoryPath); directoryPath = LocalDisk.NormalizePath(directoryPath); IDirectory directory = null; if (!Exists(path + directoryPath)) { var dir = new DirectoryInfo(path + directoryPath); dir.Create(); } directory = new Directory(path + directoryPath, disk); return(directory); }
/// <summary> /// 返回此目录的子目录(如果存在,反之抛出一个异常) /// </summary> /// <param name="directoryPath">子目录路径</param> /// <returns>子目录文件夹</returns> public IDirectory this[string directoryPath] { get { LocalDisk.GuardValidatePath(directoryPath); directoryPath = LocalDisk.NormalizePath(directoryPath); if (Exists(directoryPath)) { return(new Directory(path + directoryPath, disk)); } else { throw new DirectoryNotFoundException("a directory was not found at " + directoryPath); } } }
/// <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(); }
/// <summary> /// 文件 /// </summary> public File(string path, LocalDisk disk) { this.disk = disk; ParseFile(path); }
/// <summary> /// 构建一个文件夹 /// </summary> /// <param name="directoryPath">默认根路径</param> /// <param name="disk">本地磁盘</param> public Directory(string directoryPath, LocalDisk disk) { LocalDisk.GuardValidatePath(directoryPath); this.disk = disk; path = directoryPath; }