파일명 복사는 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;
}
'C# > 문법 및 기능' 카테고리의 다른 글
C# 퍼센트 구하는 함수 (1) | 2015.12.02 |
---|---|
C# 파일 다중선택 열기(OpenFileDialog) (3) | 2015.12.01 |
C# HttpWebRequest 를 이용한 POST 전송하기 (0) | 2015.11.13 |
C# URL 경로 링크 (0) | 2015.11.10 |
C# Timer.Tick 이벤트, 이벤트 분석 (0) | 2015.10.22 |