'C# 파일 복사(File Copy)'에 해당되는 글 1건

728x90
파일명 변경은 System.IO.File.Move(oldname, newname);

파일명 복사는 System.IO.File.Copy(oldname, newname);


oldFileName 은 파일경로와 파일명이 포함된 문자열


private string FileCopy(string oldFileName)
{
    string filePath = Path.GetDirectoryName(oldFileName);  // 파일 경로
    string filename_without_ext = Path.GetFileNameWithoutExtension(oldFileName);  // 파일 이름
    string ext_only = Path.GetExtension(oldFileName);  // 파일 확장자
    string newFileName = filePath + "\\" + filename_without_ext + "_Temp" + ext_only;
    if (File.Exists(newFileName))  // 파일의 존재 유무 확인 : 파일이 존재하면
    {
        string message = string.Format("{0} 파일이 이미 있습니다. 바꾸시겠습니까?", Path.GetFileName(newFileName));
        DialogResult dialogResult = MessageBox.Show(message, "알림", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            File.Delete(newFileName);  // 존재하는 파일 삭제
            File.Copy(oldFileName, newFileName);
        }
        else
        {
            DialogResult changeResult = MessageBox.Show("다른 파일로 변경하시겠습니까?", "알림", MessageBoxButtons.YesNo);
            if (changeResult == DialogResult.Yes)
            {
                SaveAsFile(listView2);  // SaveDialog 를 이용한 파일 저장
            }
            else
            {
                MessageBox.Show("파일명 변경을 취소하셨습니다");
            }
            newFileName = string.Empty;  // SaveAsFile 함수에서 이미 파일명 변경했거나 저장 취소
        }
    }
    else  
// 파일의 존재하지 않으면   

   {
        File.Copy(oldFileName, newFileName);
    }
    return newFileName;
}

블로그 이미지

Link2Me

,