C#通過遞歸實現文件及文件夾拷貝

enpc 9年前發布 | 2K 次閱讀 C#

         //獲得長路徑的相對短路徑名稱
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
        //實現文件拷貝,遞歸逐文件拷貝
        private void DirectoryCopy(string sourceDir, string targetDir)
        {

        if (!Directory.Exists(sourceDir))
        {
            DialogResult OKButtonDown = MessageBox.Show("需要備份的路徑不存在,請查看!", "備份失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (DialogResult.OK == OKButtonDown)
            {
                Application.Exit();
            }
        }

        string sourceFolderName = sourceDir.Replace(Directory.GetParent(sourceDir).ToString(), "").Replace(Path.DirectorySeparatorChar.ToString(), "");

        if (sourceDir == targetDir + sourceFolderName)
        {
            DialogResult OKButtonDown = MessageBox.Show("備份的目標路徑和原路徑相同,不需要備份!", "備份失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (DialogResult.OK == OKButtonDown)
            {
                Application.Exit();
            }

        }

        //得到要復制到的路徑
        string tagetPath = targetDir + Path.DirectorySeparatorChar.ToString() + sourceFolderName;


        //檢查目標路徑
        if (Directory.Exists(tagetPath))
        {
            DialogResult OKButtonDown = MessageBox.Show("備份的目標路徑已經存在,點擊是將刪除目標并備份,點擊否將覆蓋并備份", "請查看", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
            if (DialogResult.Yes == OKButtonDown)
            {
                Directory.Delete(tagetPath, true);
                //return;
            }
            else if (DialogResult.No == OKButtonDown)
            {
                //復制文件
                string[] files1 = Directory.GetFiles(sourceDir);
                for (int i = 0; i < files1.Length; i++)
                {

                    File.Copy(files1[i], tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files1[i]), true);
                }
                //復制目錄
                string[] dires1 = Directory.GetDirectories(sourceDir);
                for (int j = 0; j < dires1.Length; j++)
                {
                    DirectoryCopy(dires1[j], tagetPath);
                }
                return;
            }
            else if (DialogResult.Cancel == OKButtonDown)
            {
                Application.Exit();

            }

        }

        StringBuilder _shortPath1 = new StringBuilder(255);
        uint result1 = GetShortPathName(tagetPath, _shortPath1, 255);
        string MyShortPath1 = _shortPath1.ToString();
        if (0 == result1)
        {
            Directory.CreateDirectory(tagetPath);
        }
        else
        {
            Directory.CreateDirectory(MyShortPath1);
        }
        //復制文件
        string[] files3 = Directory.GetFiles(sourceDir);
        for (int i = 0; i < files3.Length; i++)
        {
            //string longPath = tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files3[i]);
            //StringBuilder _shortPath = new StringBuilder(255);
            //uint result = GetShortPathName(longPath, _shortPath, 255);
            //string MyShortPath = _shortPath.ToString();
            File.Copy(files3[i],/*MyShortPath/tagetPath + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(files3[i]), true);
        }
        //復制目錄
        string[] dires3 = Directory.GetDirectories(sourceDir);
        for (int j = 0; j < dires3.Length; j++)
        {
            string longPath = dires3[j];
            StringBuilder _shortPath = new StringBuilder(255);
            uint result = GetShortPathName(longPath, _shortPath, 255);
            string MyShortPath = _shortPath.ToString();

            DirectoryCopy(MyShortPath, tagetPath);
        }
    }</pre> 


 本文由用戶 enpc 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!