当前位置:编程学习 > C#/ASP.NET >>

文件目录操作类

using System;
using System.Web;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.IO.Compression;

namespace Pub.Class
{
    /// <summary>
    /// 文件目录操作类
    /// </summary>
    public class FileFolder
    {
        #region CreateFolder/CreateDir
        /// <summary>
        /// 目录不存在时新建
        /// </summary>
        /// <param name="filePath">目录(相对路径)</param>
        public static void CreateFolder(string filePath)
        {
            string[] PathArr = filePath.Split(new string[] { "/" }, StringSplitOptions.None);
            string _path = PathArr[0];
            for (int i = 1; i < PathArr.Length; i++) {
                _path = _path + "/" + PathArr[i];
                string _filePath = HttpContext.Current.Server.MapPath(_path);
                if (!System.IO.Directory.Exists(_filePath)) {
                    System.IO.Directory.CreateDirectory(_filePath);
                }
            }
        }
  /// <summary>
  /// 创建目录
  /// </summary>
  /// <param name="name">名称</param>
  /// <returns>创建是否成功</returns>
  [DllImport("dbgHelp", SetLastError=true)]
  private static extern bool MakeSureDirectoryPathExists(string name);
        /// <summary>
  /// 建立文件夹
  /// </summary>
  /// <param name="name"></param>
  /// <returns></returns>
  public static bool CreateDir(string name)
  {
   return MakeSureDirectoryPathExists(name);
  }
        /// <summary>
        /// 目录是否存在
        /// </summary>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        public static bool FolderExists(string folderPath) {
            return System.IO.Directory.Exists(folderPath);
        }
        #endregion

        #region FileExists/DelAllFile/DelFile/GetAllFile/BackupFile
        /// <summary>
  /// 返回文件是否存在
  /// </summary>
  /// <param name="filename">文件名</param>
  /// <returns>是否存在</returns>
  public static bool FileExists(string filename)
  {
   return System.IO.File.Exists(filename);
  }
        /// <summary>
        /// 删除指定目录下的所有文件
        /// </summary>
        /// <param name="path"></param>
        public static void DelAllFile(string path) {
            DirectoryInfo Folder = new DirectoryInfo(path);
      FileInfo[] subFiles = Folder.GetFiles();
      for (int j = 0; j < subFiles.Length; j++) {
                subFiles[j].Delete();
            }
        }
        /// <summary>
        /// 删除指定文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void DelFile(string filePath) {
            if (FileExists(filePath)) System.IO.File.Delete(filePath);
        }
        /// <summary>
        /// 取指定目录下的所有文件名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static StringBuilder GetAllFile(string path) {
            StringBuilder fileList = new StringBuilder();
            try {
                DirectoryInfo Folder = new DirectoryInfo(path);
                FileInfo[] subFiles = Folder.GetFiles();
                for (int j = 0; j < subFiles.Length; j++) {
                    fileList.Append(subFiles[j].Name + "|");
                }
            } catch { }
            return fileList.Length.Equals(0) ? fileList : fileList.Remove(fileList.Length - 1, 1);
        }
        /// <summary>
        /// 取目录下的所有文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileList"></pa

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,