Exemplo n.º 1
0
        /// <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, System.IO.Stream contentStream)
        {
            if (contentStream == null || !contentStream.CanRead)
            {
                return(null);
            }

            if (!IsValidPathAndFileName(relativePath, fileName))
            {
                throw new InvalidOperationException("The provided path and/or file name is invalid.");
            }

            string fullPath = GetFullLocalPath(relativePath, fileName);

            EnsurePathExists(fullPath, true);

            contentStream.Position = 0;
            using (FileStream outStream = File.OpenWrite(fullPath))
            {
                byte[] buffer = new byte[contentStream.Length > 65536 ? 65536 : contentStream.Length];

                int readedSize;
                while ((readedSize = contentStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, readedSize);
                }

                outStream.Flush();
                outStream.Close();
            }

            DefaultStoreFile file = new DefaultStoreFile(relativePath, new FileInfo(fullPath));

            return(file);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取文件路径中的所有文件
        /// </summary>
        /// <exception cref="ArgumentException">文件路径不正确时抛出异常</exception>
        /// <param name="relativePath">相对文件路径</param>
        /// <param name="isOnlyCurrentFolder">是否只获取当前层次的文件</param>
        public IEnumerable <IStoreFile> GetFiles(string relativePath, bool isOnlyCurrentFolder)
        {
            if (!DefaultStoreProvider.IsValidPath(relativePath))
            {
                throw new ArgumentException("The provided path is invalid", "relativePath");
            }

            List <IStoreFile> files     = new List <IStoreFile>();
            string            localPath = GetFullLocalPath(relativePath, string.Empty);

            if (Directory.Exists(localPath))
            {
                SearchOption searchOption = SearchOption.TopDirectoryOnly;
                if (!isOnlyCurrentFolder)
                {
                    searchOption = SearchOption.AllDirectories;
                }

                foreach (FileInfo file in (new DirectoryInfo(localPath)).GetFiles("*.*", searchOption))
                {
                    if ((file.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                    {
                        DefaultStoreFile fsFile;
                        if (isOnlyCurrentFolder)
                        {
                            fsFile = new DefaultStoreFile(relativePath, file);
                        }
                        else
                        {
                            fsFile = new DefaultStoreFile(GetRelativePath(file.FullName, true), file);
                        }

                        files.Add(fsFile);
                    }
                }
            }
            return(files);
        }