中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#通過遞歸實(shí)現(xiàn)文件及文件夾拷貝

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
         //獲得長路徑的相對短路徑名稱
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, int cchBuffer);
        //實(shí)現(xiàn)文件拷貝,遞歸逐文件拷貝
        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("備份的目標(biāo)路徑和原路徑相同,不需要備份!", "備份失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (DialogResult.OK == OKButtonDown)
                {
                    Application.Exit();
                }
  
            }
 
            //得到要復(fù)制到的路徑
            string tagetPath = targetDir + Path.DirectorySeparatorChar.ToString() + sourceFolderName;
 
 
            //檢查目標(biāo)路徑
            if (Directory.Exists(tagetPath))
            {
                DialogResult OKButtonDown = MessageBox.Show("備份的目標(biāo)路徑已經(jīng)存在,點(diǎn)擊是將刪除目標(biāo)并備份,點(diǎn)擊否將覆蓋并備份", "請查看", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (DialogResult.Yes == OKButtonDown)
                {
                    Directory.Delete(tagetPath, true);
                    //return;
                }
                else if (DialogResult.No == OKButtonDown)
                {
                    //復(fù)制文件
                    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);
                    }
                    //復(fù)制目錄
                    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);
            }
            //復(fù)制文件
            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);
            }
            //復(fù)制目錄
            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);
            }
        }

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#最小化winform程序到系統(tǒng)托盤的代碼

下一篇:C#中實(shí)現(xiàn)自定義事件的代碼演示