/// <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> /// 通过文件路径及文件名称删除文件. /// </summary> /// <param name="relativePath">文件路径</param> /// <param name="fileName">文件名称</param> public void DeleteFile(string relativePath, string fileName) { if (!DefaultStoreProvider.IsValidPathAndFileName(relativePath, fileName)) { throw new InvalidOperationException("The provided path and/or file name is invalid"); } string fullLocalPath = this.GetFullLocalPath(relativePath, fileName); if (File.Exists(fullLocalPath)) { File.Delete(fullLocalPath); } }
/// <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(); } } }
/// <summary> /// 创建或更新一个文件 /// </summary> /// <param name="relativePath">相对文件路径</param> /// <param name="fileName">文件名称</param> /// <param name="contentStream">The stream containing the content of the file.</param> public IStoreFile AddOrUpdateFile(string relativePath, string fileName, Stream contentStream) { if (contentStream == null || !contentStream.CanRead) { return(null); } if (!DefaultStoreProvider.IsValidPathAndFileName(relativePath, fileName)) { throw new InvalidOperationException("The provided path and/or file name is invalid."); } string fullLocalPath = this.GetFullLocalPath(relativePath, fileName); DefaultStoreProvider.EnsurePathExists(fullLocalPath, true); contentStream.Position = (long)0; using (FileStream fileStream = File.OpenWrite(fullLocalPath)) { byte[] numArray = new byte[ (int) checked ( (IntPtr)((contentStream.Length > (long)65536 ? (long)65536 : contentStream.Length)))]; while (true) { int num = contentStream.Read(numArray, 0, (int)numArray.Length); int num1 = num; if (num <= 0) { break; } fileStream.Write(numArray, 0, num1); } fileStream.Flush(); fileStream.Close(); } return(new DefaultStoreFile(relativePath, new FileInfo(fullLocalPath))); }