C#實現文件Move操做和文件的Copy操做

文件移動(Move)操做和文件的複製(Copy)是C#程式開發常常遇到的方法,根據傳入的源文件地址和目標文件地址參數,實現對文件的操做。實現代碼以下:ide

  1. Move操做代碼:
    public static void MoveFolder(string sourcePath, string destPath)
            {
                if (Directory.Exists(sourcePath))
                {
                    if (!Directory.Exists(destPath))
                    {
                        //目標目錄不存在則建立  
                        try
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("建立目標目錄失敗:" + 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必需要在同一個根目錄下移動纔有效,不能在不一樣卷中移動。  
                        //Directory.Move(c, destDir);  
    
                        //採用遞歸的方法實現  
                        MoveFolder(c, destDir);
                    });
                }
                else
                {
           
    Move
  2. Copy操做代碼:
    public static void CopyFilefolder(string sourceFilePath, string targetFilePath)
            {
                //獲取源文件夾中的全部非目錄文件
                string[] files = Directory.GetFiles(sourceFilePath);
                string fileName;
                string destFile;
                //若是目標文件夾不存在,則新建目標文件夾
                if (!Directory.Exists(targetFilePath))
                {
                    Directory.CreateDirectory(targetFilePath);
                }
                //將獲取到的文件一個一個拷貝到目標文件夾中  
                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);
                //建立Directoryinfo實例 
                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();
                    //把獲得的子文件夾當成新的源文件夾,遞歸調用CopyFilefolder
                    CopyFilefolder(subSourcePath, subTargetPath);
                }
            }
    Copy
相關文章
相關標籤/搜索