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