一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

C#文件操作源碼遞歸復制一個目錄遞歸刪除文件和文件夾

 悟靜 2011-08-09

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Common.Files
{
    public class FileOperator
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="path">文件路徑</param>
        /// <param name="encoding">編碼</param>
        /// <returns>文件內(nèi)容</returns>
        public string ReadFile(string path, System.Text.Encoding encoding)
        {
            string tempContent = "";
            using (StreamReader sr = new StreamReader(path, encoding))
            {
                tempContent = sr.ReadToEnd();
            }
            return tempContent;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="path">文件的路徑</param>
        /// <param name="append">是否追加</param>
        /// <param name="encoding">編碼</param>
        /// <param name="content">內(nèi)容</param>
        /// <returns>是否成功</returns>
        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;
        }
        /// <summary>
        /// 拷貝一個路徑下的文件夾和文件
        /// </summary>
        /// <param name="olddirectory"></param>
        /// <param name="newdirectory"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 遞歸拷貝一個文件夾的所有文件
        /// </summary>
        /// <param name="olddirectory"></param>
        /// <param name="newdirectory"></param>
        /// <param name="dir"></param>
        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));

                }
                catch (Exception ex)
                {
                    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++)
                {
                    //創(chuàng)建目錄
                    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);
                }
            }


        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="oldFilePath"></param>
        /// <param name="newFilePath"></param>
        /// <returns></returns>
        public bool CopyFile(string oldFilePath, string newFilePath)
        {
            bool state = false;
            File.Copy(oldFilePath, newFilePath);
            return state;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public bool DelteDirectory(string directory)
        {
            bool state = false;
            DeleteDirectoryAndFiles(directory);
            return state;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        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();
               }
           }
        }
        /// <summary>
        /// 刪除文件夾的所有文件
        /// </summary>
        /// <param name="path"></param>
        protected void DeleteFiles(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fis = di.GetFiles();
            foreach (FileInfo fi in fis)
            {
                try
                {
                    fi.Delete();
                }
                catch
                {
                    continue;
                }
            }

        }
      

       

    }
}

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    国产精品刮毛视频不卡| 在线观看中文字幕91| 天堂网中文字幕在线观看| 中文文精品字幕一区二区| 国产麻豆成人精品区在线观看| 久久大香蕉精品在线观看| 欧美乱视频一区二区三区| 国产精品免费视频视频| 成人午夜激情在线免费观看| 人妻露脸一区二区三区| 91亚洲熟女少妇在线观看| 欧美一区二区三区五月婷婷| 都市激情小说在线一区二区三区| 欧美日韩免费观看视频| 99热中文字幕在线精品| 国产亚洲精品一二三区| 欧美日韩久久精品一区二区| 男人大臿蕉香蕉大视频| 好吊色欧美一区二区三区顽频| 国产免费一区二区不卡| 亚洲精品中文字幕一二三| 国产又粗又猛又大爽又黄| 91日韩在线观看你懂的| 欧美国产精品区一区二区三区| 国产精品夜色一区二区三区不卡| 国产二级一级内射视频播放| 欧美精品一区久久精品| 东京热男人的天堂久久综合| 久久国产精品亚州精品毛片| 一区二区三区欧美高清| 欧美成人精品国产成人综合| 少妇成人精品一区二区| 亚洲国产精品久久网午夜| 视频一区二区黄色线观看| 国产福利一区二区久久| 国产精品亚洲综合色区韩国| 久久精品国产99精品最新| 欧美国产日本高清在线| 欧美日韩亚洲精品在线观看| 国产日韩欧美国产欧美日韩| 亚洲精品中文字幕熟女|