1. 程式人生 > >C# winform資料夾操作複製和刪除

C# winform資料夾操作複製和刪除

首先需要新增名稱空間:

using System.IO;

下面分別有2個刪除方法:
        /// <summary>
        /// 刪除資料夾下所有檔案
        /// </summary>
        /// <param name="dir"></param>
        public void DeleteFolder(string dir)
        {
            //如果存在這個資料夾刪除之 
            if (Directory.Exists(dir))
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d);//直接刪除其中的檔案 
                    else DeleteFolder(d);//遞迴刪除子資料夾  
                }
                Directory.Delete(dir);
                //刪除已空資料夾 
                MessageBox.Show(dir + "資料夾刪除成功");
            }
            else //如果資料夾不存在則提示 
                MessageBox.Show(dir + "該資料夾不存在");
        }

        //========================================  
        //實現一個靜態方法將指定資料夾下面的所有內容Detele
        //測試的時候要小心操作,刪除之後無法恢復。
        //========================================
        public static void DeleteDir(string aimPath)
        {
            try
            {
                //檢查目標目錄是否以目錄分割字元結束如果不是則新增之
                if (aimPath[aimPath.Length - 1] !=
                    Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                //得到源目錄的檔案列表,該裡面是包含檔案以及目錄路徑的一個數組
                //如果你指向Delete目標檔案下面的檔案而不包含目錄請使用下面的方法
                //string[]fileList=  Directory.GetFiles(aimPath);
                string[] fileList = Directory.GetFileSystemEntries(aimPath);
                //遍歷所有的檔案和目錄 
                foreach (string file in fileList)
                {
                    //先當作目錄處理如果存在這個
                    //目錄就遞迴Delete該目錄下面的檔案 
                    if (Directory.Exists(file))
                    {
                        DeleteDir(aimPath + Path.GetFileName(file));
                    }
                    //否則直接Delete檔案 
                    else
                    {
                        File.Delete(aimPath + Path.GetFileName(file));
                    }
                }
                //刪除資料夾 
                System.IO.Directory.Delete(aimPath, true);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

下面是2個複製方法:
        ///拷貝資料夾(包括子資料夾)到指定資料夾下
        ///原始檔夾和目標資料夾均需絕對路徑
        ///格式:CopyFolder(原始檔夾,目標資料夾)
        //----------------------------------------
        //作者 14
        //----------------------------------------
        /// <summary>
        /// 拷貝資料夾(包括子資料夾)到指定資料夾下
        /// 檔案和資料夾分開復制,當是資料夾則遞迴複製
        /// </summary>
        /// <param name="strFromPath">待複製地址</param>
        /// <param name="strToPath">目標地址</param>
        public static void CopyFolder(string strFromPath,string strToPath)
        {
            //如果原始檔夾不存在,則建立  
            if (!Directory.Exists(strFromPath))
            {
                Directory.CreateDirectory(strFromPath);
            }
            //取得要拷貝的資料夾名  
            string strFolderName = strFromPath.Substring(
                strFromPath.LastIndexOf("\\") + 1,
                strFromPath.Length -
                strFromPath.LastIndexOf("\\") - 1);
            //如果目標資料夾中沒有原始檔夾
            //則在目標資料夾中建立原始檔夾 
            if (!Directory.Exists(
                strToPath + "\\" + strFolderName))
            {
                Directory.CreateDirectory(
                    strToPath + "\\" + strFolderName);
            }
            //建立陣列儲存原始檔夾下的檔名  
            string[] strFiles =
                Directory.GetFiles(strFromPath);
            //迴圈拷貝檔案 
            for (int i = 0; i < strFiles.Length; i++)
            {
                //取得拷貝的檔名,只取檔名,地址截掉。
                string strFileName = strFiles[i].Substring(
                    strFiles[i].LastIndexOf("\\") + 1,
                    strFiles[i].Length -
                    strFiles[i].LastIndexOf("\\") - 1);
                //開始拷貝檔案,true表示覆蓋同名檔案  
                File.Copy(
                    strFiles[i],
                    strToPath + "\\" + strFolderName +
                    "\\" + strFileName,
                    true);
            }
            //建立DirectoryInfo例項  
            DirectoryInfo dirInfo =
                new DirectoryInfo(strFromPath);
            //取得原始檔夾下的所有子資料夾名稱 
            DirectoryInfo[] ZiPath =
                dirInfo.GetDirectories();
            for (int j = 0; j < ZiPath.Length; j++)
            {
                //獲取所有子資料夾名  
                string strZiPath = strFromPath + "\\" +
                    ZiPath[j].ToString();
                //把得到的子資料夾當成新的
                //原始檔夾,從頭開始新一輪的拷貝
                CopyFolder(
                    strZiPath,
                    strToPath + "\\" + strFolderName);
            }
        }

        //========================================
        //實現一個靜態方法將指定資料夾下面的所有
        //內容copy到目標資料夾下面
        //如果目標資料夾為只讀屬性就會報錯
        //========================================
        public void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                //檢查目標目錄是否以目錄分割字元
                //結束如果不是則新增之 
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                //判斷目標目錄是否存在如果不存在則新建之
                if (!Directory.Exists(aimPath))
                    Directory.CreateDirectory(aimPath);
                //得到源目錄的檔案列表,該裡面是包含
                //檔案以及目錄路徑的一個數組  
                //如果你指向copy目標檔案下面的檔案  
                //而不包含目錄請使用下面的方法  
                //string[]fileList=  Directory.GetFiles(srcPath);
                string[] fileList =
                    Directory.GetFileSystemEntries(srcPath);
                //遍歷所有的檔案和目錄  
                foreach (string file in fileList)
                {
                    //先當作目錄處理如果存在這個
                    //目錄就遞迴Copy該目錄下面的檔案  
                    if (Directory.Exists(file))
                        CopyDir(
                            file,
                            aimPath + Path.GetFileName(file));
                    //否則直接Copy檔案 
                    else
                        File.Copy(
                            file,
                            aimPath + Path.GetFileName(file),
                            true);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

上面說的刪除方法,會把目錄本身刪了。

如下面這個地址:

string opath = @"C:\Documents and Settings\Administrator\桌面\211";

執行上面刪除方法,會把C:\Documents and Settings\Administrator\桌面\211中的211這個資料夾也刪了,如果不想刪,就用下面這個方法,n等於1的時候就不刪。

       //========================================  
       //實現一個靜態方法將指定資料夾下面的所有內容Detele
       //測試的時候要小心操作,刪除之後無法恢復。
        //========================================
        public static void DeleteDir(string aimPath, int n)
        {
            try
            {
                //檢查目標目錄是否以目錄分割字元結束如果不是則新增之
                if (aimPath[aimPath.Length - 1] !=
                    Path.DirectorySeparatorChar)
                    aimPath += Path.DirectorySeparatorChar;
                //得到源目錄的檔案列表,該裡面是包含檔案以及目錄路徑的一個數組
                //如果你指向Delete目標檔案下面的檔案而不包含目錄請使用下面的方法
                string[] fileList = Directory.GetFiles(aimPath);
                //string[] fileList = Directory.GetFileSystemEntries(aimPath);
                //遍歷所有的檔案和目錄 
                foreach (string file in fileList)
                {
                    //先當作目錄處理如果存在這個
                    //目錄就遞迴Delete該目錄下面的檔案 
                    if (Directory.Exists(file))
                    {
                        DeleteDir(aimPath + Path.GetFileName(file), n);
                    }
                    //否則直接Delete檔案 
                    else
                    {
                        File.Delete(aimPath + Path.GetFileName(file));
                    }
                }
                //刪除資料夾 
                if (n == 1)
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(aimPath);
                    //取得原始檔夾下的所有子資料夾名稱 
                    DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
                    for (int j = 0; j < ZiPath.Length; j++)
                    {
                        //獲取子資料夾名
                        string strZiPath = aimPath + "\\" + ZiPath[j].ToString();
                        //刪除當前資料夾
                        System.IO.Directory.Delete(strZiPath, true);
                    }
                }
                else
                {
                    System.IO.Directory.Delete(aimPath, true);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }