using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Common.Files
{
    public class FileOperator
    {
        /// 
        /// 
        /// 
        /// 文件路径
        /// 编码
        /// 文件内容
        public string ReadFile(string path, System.Text.Encoding encoding)
        {
            string tempContent = "";
            using (StreamReader sr = new StreamReader(path, encoding))
            {
                tempContent = sr.ReadToEnd();
            }
            return tempContent;
        }
        /// 
        /// 
        /// 
        /// 文件的路径
        /// 是否追加
        /// 编码
        /// 内容
        /// 是否成功
        public bool WriteFile(string path, bool append, System.Text.Encoding encoding, string content)
        {
            bool state = false;
            try
            {
                using (StreamWriter sw = new StreamWriter(path, append, encoding))
                {
                    sw.Write(content);
                    sw.Flush();
                    state = true;
                }
            }
            catch
            {
            }
            return state;
        }
        /// 
        /// 拷贝一个路径下的文件夹和文件
        /// 
        /// 
        /// 
        /// 
        public bool CopyDirectoryAndFiles(string olddirectory, string newdirectory)
        {
            bool state = false;
            try
            {
                DirectoryInfo dis = new DirectoryInfo(newdirectory);
                if (!dis.Exists)
                {
                    Directory.CreateDirectory(newdirectory);
                }
                CopyAll(olddirectory, newdirectory, "");
                state = true;
            }
            catch
            {
            }
            return state;
        }
        /// 
        /// 递归拷贝一个文件夹的所有文件
        /// 
        /// 
        /// 
        /// 
        protected void CopyAll(string olddirectory, string newdirectory, string dir)
        {
            DirectoryInfo dis = new DirectoryInfo(dir.Length > 0 ? (dir) : (olddirectory));
            FileInfo[] files = dis.GetFiles();
            foreach (FileInfo fi in files)
            {
                try
                {
                    //拷贝文件
                    File.Copy(fi.FullName, string.Format("{0}\\{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), fi.Name),true);
                }
                catch
                {
                    try
                    {
                        File.Delete(string.Format("{0}\\{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), fi.Name));
                        //string temp = ex.Message;
                        File.Copy(fi.FullName, string.Format("{0}\\{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), fi.Name));
                    }
                    catch
                    {
                    }
                }
            }
            if (dis.GetDirectories().Length > 0)
            {
                for (int i = 0; i < dis.GetDirectories().Length; i++)
                {
                    //创建目录
                    string tempNewPath = string.Format("{0}\\{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), dis.GetDirectories()[i].Name);
                    if (!Directory.Exists(tempNewPath))
                        Directory.CreateDirectory(tempNewPath);
                    CopyAll(olddirectory, newdirectory, dis.GetDirectories()[i].FullName);
                }
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool CopyFile(string oldFilePath, string newFilePath)
        {
            bool state = false;
            File.Copy(oldFilePath, newFilePath);
            return state;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool DelteDirectory(string directory)
        {
            bool state = false;
            DeleteDirectoryAndFiles(directory);
            return state;
        }
        /// 
        /// 
        /// 
        /// 
        protected void DeleteDirectoryAndFiles(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.GetFiles().Length > 0)
                DeleteFiles(path);
            DirectoryInfo[] dis = di.GetDirectories();
            if (dis.Length > 0)
            {
                foreach (DirectoryInfo tempdi in dis)
                {
                    DeleteDirectoryAndFiles(tempdi.FullName);
                    if (tempdi.GetFiles().Length < 1 && tempdi.GetDirectories().Length < 1)
                        tempdi.Delete();
                }
            }
        }
        /// 
        /// 删除文件夹的所有文件
        /// 
        /// 
        protected void DeleteFiles(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fis = di.GetFiles();
            foreach (FileInfo fi in fis)
            {
                try
                {
                    fi.Delete();
                }
                catch
                {
                    continue;
                }
            }
        }
    }
}