2012-03-12 : 초안 작성
2017-12-30 : 소스코드 및 설명 추가
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
/* | |
* @brief : DirectoryInfo class 를 사용하기 위함 | |
*/ | |
using System.IO; | |
namespace LearningCSharp.util | |
{ | |
class FileUtils | |
{ | |
/* | |
* @brief : 디렉토리 삭제 메소드 | |
* @arg path : 디렉토리 경로 | |
*/ | |
public static bool deleteDirectory(string path) | |
{ | |
bool isDelete = false; | |
DirectoryInfo tempDirInfo = new DirectoryInfo(path); | |
if (tempDirInfo.Exists == true) | |
{ | |
foreach (DirectoryInfo di in tempDirInfo.GetDirectories()) | |
{ | |
foreach (FileInfo fi in di.GetFiles()) | |
{ | |
// 파일 속성이 ReadOnly 인 경우에 노멀로 변경 하고 삭제 한다. | |
if ((fi.Attributes & FileAttributes.ReadOnly) > 0) | |
{ | |
fi.Attributes = FileAttributes.Normal; | |
} | |
} | |
} | |
tempDirInfo.Delete(true); | |
isDelete = true; | |
} | |
return isDelete; | |
} | |
} | |
} |
읽기 전용 디렉토리 삭제하는 방법입니다.
DirectoryInfo, FileInfo class를 사용하기 위해서 최상단에 using.System.IO 를 추가 합니다.
FileInfo class 를 사용하여 해당 파일에 속성을 확인 하여 읽기 전용 파일이면 파일 속성을 FileAtributes.Normal로 변경 합니다.
private void button1_Click(object sender, EventArgs e) | |
{ | |
string path = "c:/test"; | |
bool isSuccess = FileUtils.deleteDirectory(path); | |
if (isSuccess) { | |
MessageBox.Show(path + " 삭제 완료"); | |
} | |
else { | |
MessageBox.Show(path + " 에 폴더가 없습니다."); | |
} | |
} |
메시지 박스 사용하는 방법 입니다.
MessageBox.Show("출력문자") Show 메소드 파라미터에 원하는 문자열을 사용 하면 됩니다.
풀 소스는 아래에 있습니다.
ASP.NET - ADO.NET + ASP.NET DB 연동 간단한 예제 (0) | 2018.01.02 |
---|---|
C# - 들여쓰기 (0) | 2017.12.31 |
C# - error CS0227: Unsafe code (0) | 2017.12.30 |
C# - 크로스 스레드 작업이 잘못 되었습니다. (0) | 2017.12.30 |
C# - 디렉토리 및 파일 생성 (0) | 2015.08.02 |
댓글 영역