프로그래밍/.NetFramework
폴더복사
쇠주는참이슬
2014. 2. 10. 09:59
폴더내에 있는 모든 파일들을 하위파일/폴더 포함해서 싸그리 복사해서
다른 경로에 붙여넣는 클래스.
파일만 복사할꺼면 살짝? 만 손보면 됨.
복사가 되는 상태를 파일별로 보여주고 싶다면 BackgroundWorker를 이용해서 비동기로 작업하면 해결됨.
복사가 되는 상태를 파일별로 보여주고 싶다면 BackgroundWorker를 이용해서 비동기로 작업하면 해결됨.
///사용법/// 디렉토리 복사 /// /// 복사할 디렉토리 /// 복여넣을 디렉토리 /// true : 덮어씀, false : 덮어쓰지 않음 ///true :성공, false : 실패 private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting) { bool ret = true; try { SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\"; if (Directory.Exists(SourcePath)) { if (Directory.Exists(DestinationPath) == false) Directory.CreateDirectory(DestinationPath); foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo flinfo = new FileInfo(fls); flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false) ret = false; } } else { ret = false; } } catch (Exception) { ret = false; } return ret; }
///폴더복사, 파일복사, 복사, filecopy/// 실제 복사작업이 이루어지는 비동기 함수 /// /// /// void worker_DoWork(object sender, DoWorkEventArgs e) { Cursor = Cursors.WaitCursor; string sourcePath = txtCopy.Text; // 복사할 폴더경로 string targetPath = txtPaste.Text; // 붙여넣을 폴더경로 CopyDirectory(sourcePath, targetPath, true); }