FileManager(文件管理类)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;

[Serializable]
public class FileItem
{
    public FileItem()
    { }

    #region 私有字段
    private string _Name;
    private string _FullName;
    private DateTime _CreationDate;
    private bool _IsFolder;
    private long _Size;
    private DateTime _LastAccessDate;
    private DateTime _LastWriteDate;
    private int _FileCount;
    private int _SubFolderCount;
    #endregion

    #region 公有属性
    ///


    /// 名称
    ///

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    ///


    /// 文件或目录的完整目录
    ///

    public string FullName
    {
        get { return _FullName; }
        set { _FullName = value; }
    }

    ///


    ///  创建时间
    ///

    public DateTime CreationDate
    {
        get { return _CreationDate; }
        set { _CreationDate = value; }
    }

    public bool IsFolder
    {
        get { return _IsFolder; }
        set { _IsFolder = value; }
    }

    ///


    /// 文件大小
    ///

    public long Size
    {
        get { return _Size; }
        set { _Size = value; }
    }

    ///


    /// 上次访问时间
    ///

    public DateTime LastAccessDate
    {
        get { return _LastAccessDate; }
        set { _LastAccessDate = value; }
    }

    ///


    /// 上次读写时间
    ///

    public DateTime LastWriteDate
    {
        get { return _LastWriteDate; }
        set { _LastWriteDate = value; }
    }

    ///


    /// 文件个数
    ///

    public int FileCount
    {
        get { return _FileCount; }
        set { _FileCount = value; }
    }

    ///


    /// 目录个数
    ///

    public int SubFolderCount
    {
        get { return _SubFolderCount; }
        set { _SubFolderCount = value; }
    }
    #endregion
}

public class FileManager
{
    #region 构造函数
    private static string strRootFolder;
    static FileManager()
    {
        strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath + "File\\";
        strRootFolder = strRootFolder.Substring(0, strRootFolder.LastIndexOf(@"\"));
    }
    #endregion

    #region 目录
    ///


    /// 读根目录
    ///

    public static string GetRootPath()
    {
        return strRootFolder;
    }

    ///


    /// 写根目录
    ///

    public static void SetRootPath(string path)
    {
        strRootFolder = path;
    }

    ///


    /// 读取目录列表
    ///

    public static List GetDirectoryItems()
    {
        return GetDirectoryItems(strRootFolder);
    }

    ///


    /// 读取目录列表
    ///

    public static List GetDirectoryItems(string path)
    {
        List list = new List();
        string[] folders = Directory.GetDirectories(path);
        foreach (string s in folders)
        {
            FileItem item = new FileItem();
            DirectoryInfo di = new DirectoryInfo(s);
            item.Name = di.Name;
            item.FullName = di.FullName;
            item.CreationDate = di.CreationTime;
            item.IsFolder = false;
            list.Add(item);
        }
        return list;
    }
    #endregion

    #region 文件
    ///


    /// 读取文件列表
    ///

    public static List GetFileItems()
    {
        return GetFileItems(strRootFolder);
    }

    ///


    /// 读取文件列表
    ///

    public static List GetFileItems(string path)
    {
        List list = new List();
        string[] files = Directory.GetFiles(path);
        foreach (string s in files)
        {
            FileItem item = new FileItem();
            FileInfo fi = new FileInfo(s);
            item.Name = fi.Name;
            item.FullName = fi.FullName;
            item.CreationDate = fi.CreationTime;
            item.IsFolder = true;
            item.Size = fi.Length;
            list.Add(item);
        }
        return list;
    }

    ///


    /// 创建文件
    ///

    public static bool CreateFile(string filename, string path)
    {
        try
        {
            FileStream fs = File.Create(path + "\\" + filename);
            fs.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 创建文件
    ///

    public static bool CreateFile(string filename, string path, byte[] contents)
    {
        try
        {
            FileStream fs = File.Create(path + "\\" + filename);
            fs.Write(contents, 0, contents.Length);
            fs.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 读取文件
    ///

    public static string OpenText(string parentName)
    {
        StreamReader sr = File.OpenText(parentName);
        StringBuilder output = new StringBuilder();
        string rl;
        while ((rl = sr.ReadLine()) != null)
        {
            output.Append(rl);
        }
        sr.Close();
        return output.ToString();
    }

    ///


    /// 读取文件信息
    ///

    public static FileItem GetItemInfo(string path)
    {
        FileItem item = new FileItem();
        if (Directory.Exists(path))
        {
            DirectoryInfo di = new DirectoryInfo(path);
            item.Name = di.Name;
            item.FullName = di.FullName;
            item.CreationDate = di.CreationTime;
            item.IsFolder = true;
            item.LastAccessDate = di.LastAccessTime;
            item.LastWriteDate = di.LastWriteTime;
            item.FileCount = di.GetFiles().Length;
            item.SubFolderCount = di.GetDirectories().Length;
        }
        else
        {
            FileInfo fi = new FileInfo(path);
            item.Name = fi.Name;
            item.FullName = fi.FullName;
            item.CreationDate = fi.CreationTime;
            item.LastAccessDate = fi.LastAccessTime;
            item.LastWriteDate = fi.LastWriteTime;
            item.IsFolder = false;
            item.Size = fi.Length;
        }
        return item;
    }

    ///


    /// 写入一个新文件,在文件中写入内容,然后关闭文件。如果目标文件已存在,则改写该文件。
    ///

    public static bool WriteAllText(string parentName, string contents)
    {
        try
        {
            File.WriteAllText(parentName, contents, Encoding.Unicode);
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 删除文件
    ///

    public static bool DeleteFile(string path)
    {
        try
        {
            File.Delete(path);
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 移动文件
    ///

    public static bool MoveFile(string oldPath, string newPath)
    {
        try
        {
            File.Move(oldPath, newPath);
            return true;
        }
        catch
        {
            return false;
        }
    }
    #endregion

    #region 文件夹
    ///


    /// 创建文件夹
    ///

    public static void CreateFolder(string name, string parentName)
    {
        DirectoryInfo di = new DirectoryInfo(parentName);
        di.CreateSubdirectory(name);
    }

    ///


    /// 删除文件夹
    ///

    public static bool DeleteFolder(string path)
    {
        try
        {
            Directory.Delete(path);
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 移动文件夹
    ///

    public static bool MoveFolder(string oldPath, string newPath)
    {
        try
        {
            Directory.Move(oldPath, newPath);
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 复制文件夹
    ///

    public static bool CopyFolder(string source, string destination)
    {
        try
        {
            String[] files;
            if (destination[destination.Length - 1] != Path.DirectorySeparatorChar) destination += Path.DirectorySeparatorChar;
            if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);
            files = Directory.GetFileSystemEntries(source);
            foreach (string element in files)
            {
                if (Directory.Exists(element))
                    CopyFolder(element, destination + Path.GetFileName(element));
                else
                    File.Copy(element, destination + Path.GetFileName(element), true);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    #endregion

    #region 检测文件
    ///


    /// 判断是否为安全文件名
    ///

    /// 文件名
    public static bool IsSafeName(string strExtension)
    {
        strExtension = strExtension.ToLower();
        if (strExtension.LastIndexOf(".") >= 0)
        {
            strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
        }
        else
        {
            strExtension = ".txt";
        }
        string[] arrExtension = { ".htm", ".html", ".txt", ".js", ".css", ".xml", ".sitemap", ".jpg", ".gif", ".png", ".rar", ".zip" };
        for (int i = 0; i < arrExtension.Length; i++)
        {
            if (strExtension.Equals(arrExtension[i]))
            {
                return true;
            }
        }
        return false;
    }

    ///


    ///  判断是否为不安全文件名
    ///

    /// 文件名、文件夹名
    public static bool IsUnsafeName(string strExtension)
    {
        strExtension = strExtension.ToLower();
        if (strExtension.LastIndexOf(".") >= 0) 
        {
            strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
        }
        else
        {
            strExtension = ".txt";
        }
        string[] arrExtension = { ".", ".asp", ".aspx", ".cs", ".net", ".dll", ".config", ".ascx", ".master", ".asmx", ".asax", ".cd", ".browser", ".rpt", ".ashx", ".xsd", ".mdf", ".resx", ".xsd" };
        for (int i = 0; i < arrExtension.Length; i++)
        {
            if (strExtension.Equals(arrExtension[i]))
            {
                return true;
            }
        }
        return false;
    }

    ///


    ///  判断是否为可编辑文件
    ///

    /// 文件名、文件夹名
    public static bool IsCanEdit(string strExtension)
    {
        strExtension = strExtension.ToLower();

        if (strExtension.LastIndexOf(".") >= 0)
        {
            strExtension = strExtension.Substring(strExtension.LastIndexOf("."));
        }
        else
        {
            strExtension = ".txt";
        }
        string[] arrExtension = { ".htm", ".html", ".txt", ".js", ".css", ".xml", ".sitemap" };
        for (int i = 0; i < arrExtension.Length; i++)
        {
            if (strExtension.Equals(arrExtension[i]))
            {
                return true;
            }
        }
        return false;
    }
    #endregion
}

转载于:https://www.cnblogs.com/mynameltg/p/4043461.html

你可能感兴趣的:(FileManager(文件管理类))