Dev/C#

폴더 복사...

쇼크리더 2008. 4. 11. 20:31
반응형

밑의 함수의 인자를 보시는것처러 처음에는 복사할 폴더 경로

다음인자는 복사되어지는 위치 그리고 마지막 인자는 true를 넣어주세요..ㅋㅋ


다 같이 공유하면서..ㅋㅋㅋ

그럼 수고하세요~~



        /// <summary>

        /// Floder Copy

        /// </summary>

        /// <param name="szSourceFolderPath"></param>

        /// <param name="szDestinationFolderPath"></param>

        /// <param name="bOverwrite"></param>


        private void CopyFolder(string szSourceFolderPath, string szDestinationFolderPath, bool bOverwrite)

        {

            if (Directory.Exists(szSourceFolderPath))

            {

                if (Directory.Exists(szDestinationFolderPath + "\\" + (new DirectoryInfo(szSourceFolderPath).Name)) && (!bOverwrite))

                    throw new System.Exception("이미 존재하는 폴더 입니다.\r\n " + szDestinationFolderPath);

                else if (!Directory.Exists(szDestinationFolderPath + "\\" + (new DirectoryInfo(szSourceFolderPath).Name)))

                    Directory.CreateDirectory(szDestinationFolderPath + "\\" + (new DirectoryInfo(szSourceFolderPath).Name));


                // 모든 파일

                FileInfo[] objFiA = (new DirectoryInfo(szSourceFolderPath).GetFiles());

                foreach (FileInfo objFi in objFiA)

                {

                    objFi.CopyTo(szDestinationFolderPath + "\\" + (new DirectoryInfo(szSourceFolderPath).Name) + "\\" + objFi.Name);

                }


                // 자식 폴더

                DirectoryInfo[] objDiA = (new DirectoryInfo(szSourceFolderPath).GetDirectories());

                foreach (DirectoryInfo objDi in objDiA)

                {

                    CopyFolder(objDi.FullName, szDestinationFolderPath + "\\" + (new DirectoryInfo(szSourceFolderPath).Name), bOverwrite);

                }

            }

            else

                throw new System.Exception("복사할 폴더가 존재하지 않습니다.\r\n " + szSourceFolderPath);

        }



주의!
사용하다 보니 용량이 큰 폴더인 경우, 하위디렉터리가 많은 경우 재귀함수구조로 인해 메모리를 엄청 잡아먹으면서 뻗을 수 있네요. -_-;;
win32 api를 이용한 방법을 쓰는게 좋겠네요.

반응형