程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 文件目錄操作類

文件目錄操作類

編輯:C#入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved