日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C#實(shí)現(xiàn)文件Move操作和文件的Copy操作

 路人甲Java 2020-04-24

文件移動(Move)操作和文件的復(fù)制(Copy)是C#程式開發(fā)經(jīng)常遇到的方法,根據(jù)傳入的源文件地址和目標(biāo)文件地址參數(shù),實(shí)現(xiàn)對文件的操作。實(shí)現(xiàn)代碼如下:

  1. Move操作代碼:
    public static void MoveFolder(string sourcePath, string destPath)
            {
                if (Directory.Exists(sourcePath))
                {
                    if (!Directory.Exists(destPath))
                    {
                        //目標(biāo)目錄不存在則創(chuàng)建  
                        try
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("創(chuàng)建目標(biāo)目錄失?。?/span>" + ex.Message);
                        }
                    }
                    //獲得源文件下所有文件  
                    List<string> files = new List<string>(Directory.GetFiles(sourcePath));
                    files.ForEach(c =>
                    {
                        string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                        //覆蓋模式  
                        if (File.Exists(destFile))
                        {
                            File.Delete(destFile);
                        }
                        File.Move(c, destFile);
                    });
                    //獲得源文件下所有目錄文件  
                    List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
    
                    folders.ForEach(c =>
                    {
                        string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                        //Directory.Move必須要在同一個(gè)根目錄下移動才有效,不能在不同卷中移動。  
                        //Directory.Move(c, destDir);  
    
                        //采用遞歸的方法實(shí)現(xiàn)  
                        MoveFolder(c, destDir);
                    });
                }
                else
                {
           
    Move
  2. Copy操作代碼:
    public static void CopyFilefolder(string sourceFilePath, string targetFilePath)
            {
                //獲取源文件夾中的所有非目錄文件
                string[] files = Directory.GetFiles(sourceFilePath);
                string fileName;
                string destFile;
                //如果目標(biāo)文件夾不存在,則新建目標(biāo)文件夾
                if (!Directory.Exists(targetFilePath))
                {
                    Directory.CreateDirectory(targetFilePath);
                }
                //將獲取到的文件一個(gè)一個(gè)拷貝到目標(biāo)文件夾中  
                foreach (string s in files)
                {
                    fileName = Path.GetFileName(s);
                    destFile = Path.Combine(targetFilePath, fileName);
                    File.Copy(s, destFile, true);
                }
                //上面一段在MSDN上可以看到源碼 
    
                //獲取并存儲源文件夾中的文件夾名
                string[] filefolders = Directory.GetFiles(sourceFilePath);
                //創(chuàng)建Directoryinfo實(shí)例 
                DirectoryInfo dirinfo = new DirectoryInfo(sourceFilePath);
                //獲取得源文件夾下的所有子文件夾名
                DirectoryInfo[] subFileFolder = dirinfo.GetDirectories();
                for (int j = 0; j < subFileFolder.Length; j++)
                {
                    //獲取所有子文件夾名 
                    string subSourcePath = sourceFilePath + "\\" + subFileFolder[j].ToString();
                    string subTargetPath = targetFilePath + "\\" + subFileFolder[j].ToString();
                    //把得到的子文件夾當(dāng)成新的源文件夾,遞歸調(diào)用CopyFilefolder
                    CopyFilefolder(subSourcePath, subTargetPath);
                }
            }
    Copy

     

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多