본문 바로가기
C#

C# 폴더에 있는 파일을 복사하는 소스 예제

by 샤나엘 2021. 7. 8.
반응형

특정경로에 있는 파일을 복사해주는 소스 예제입니다.

 

            string sourceFolderPath = //이 경로에 있는 폴더의 내용을

            string destinationFolderPath = //이 경로로 복사

			//경로가 없는 경우 처리
            if (System.IO.Directory.Exists(destinationFolderPath) == false || System.IO.Directory.Exists(sourceFolderPath) ==  false)
            {
                return;
            }

			//파일 내용 읽어오기
            string[] fileArray = System.IO.Directory.GetFiles(sourceFolderPath);

            foreach (string file in fileArray)
            {
            	string fileName = System.IO.Path.GetFileName(file);

              try
              {
  			      //파일명 생성
                  string destinationFileName = System.IO.Path.Combine(destinationFolderPath, fileName);

				  //파일 덮어쓰기로 복사 실행
                  System.IO.File.Copy(file, destinationFileName, true);
              }
              catch (Exception ex)
              {
                  // 오류내용 기록
              }
            }
반응형

댓글