본문 바로가기

Web_Application/C#

[c#] 해당 경로에 파일 복사하기



string fileName = "{{복사 할 파일명}}";

string sourceFilePath = "{{원본 경로}}";

string targetFilePath = "{{복사 할 경로}}";


boolean existsCheck = false;

boolean fileExists = false;

FileInfo file;


sourceFilePath = sourceFilePath + @"/" + fileName;


file = new FileInfo(sourceFilePath);


fileExists = file.Exists;


if(fileExists)

{

    try

    {

        if (!Directory.Exists(targetFilePath))

            Directory.CreateDirectory(targetFilePath);


        targetFilePath += "/" + {{복사 할 파일명}};


        existsCheck = new FileInfo(targetFilePath);


        if (existsCheck.Exists)

            existsCheck.Delete();


        file.CopyTo(targetFilePath);


        Console.WriteLine("ORG - OK :" + targetFilePath);

    }

    catch (Exception ex)

    {

        Console.WriteLine("exception file path : " + targetFilePath);

    }

} else {

    Console.WriteLine("대상파일이 존재하지 않습니다.");    

}