/// <summary> /// 获取文件路径中的所有文件 /// </summary> /// <exception cref="T:System.ArgumentException">文件路径不正确时抛出异常</exception> /// <param name="relativePath">相对文件路径</param> /// <param name="isOnlyCurrentFolder">是否只获取当前层次的文件</param> public IEnumerable <IStoreFile> GetFiles(string relativePath, bool isOnlyCurrentFolder) { DefaultStoreFile defaultStoreFile; if (!DefaultStoreProvider.IsValidPath(relativePath)) { throw new ArgumentException("The provided path is invalid", "relativePath"); } List <IStoreFile> storeFiles = new List <IStoreFile>(); string fullLocalPath = this.GetFullLocalPath(relativePath, string.Empty); if (Directory.Exists(fullLocalPath)) { SearchOption searchOption = SearchOption.TopDirectoryOnly; if (!isOnlyCurrentFolder) { searchOption = SearchOption.AllDirectories; } FileInfo[] files = (new DirectoryInfo(fullLocalPath)).GetFiles("*.*", searchOption); for (int i = 0; i < (int)files.Length; i++) { FileInfo fileInfo = files[i]; if ((fileInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) { defaultStoreFile = (!isOnlyCurrentFolder ? new DefaultStoreFile(this.GetRelativePath(fileInfo.FullName, true), fileInfo) : new DefaultStoreFile(relativePath, fileInfo)); storeFiles.Add(defaultStoreFile); } } } return(storeFiles); }
/// <summary> /// 验证文件路径以及文件名称是否合法 /// </summary> private static bool IsValidPathAndFileName(string path, string fileName) { if (!DefaultStoreProvider.IsValidPath(path) || !DefaultStoreProvider.IsValidFileName(fileName)) { return(false); } return((int)Encoding.UTF8.GetBytes(string.Concat(path, ".", fileName)).Length <= 1024); }
/// <summary> /// 删除文件目录 /// </summary> /// <param name="relativePath">相对文件路径</param> public void DeleteFolder(string relativePath) { if (!DefaultStoreProvider.IsValidPath(relativePath)) { return; } string fullLocalPath = this.GetFullLocalPath(relativePath, string.Empty); if (Directory.Exists(fullLocalPath)) { Directory.Delete(fullLocalPath, true); } }
/// <summary> /// 删除文件路径中以fileNamePrefix开头的文件 /// </summary> /// <param name="relativePath">相对文件路径</param> /// <param name="fileNamePrefix">文件名称前缀</param> public void DeleteFiles(string relativePath, string fileNamePrefix) { if (!DefaultStoreProvider.IsValidPath(relativePath)) { throw new InvalidOperationException("The provided path is invalid"); } string fullLocalPath = this.GetFullLocalPath(relativePath, string.Empty); if (Directory.Exists(fullLocalPath)) { DirectoryInfo directoryInfo = new DirectoryInfo(fullLocalPath); FileInfo[] files = directoryInfo.GetFiles(string.Concat(fileNamePrefix, "*")); for (int i = 0; i < (int)files.Length; i++) { files[i].Delete(); } } }