상세 컨텐츠

본문 제목

C# - 읽기 전용 디렉토리 삭제하기

컴퓨터 언어/C#

by cepiloth 2017. 12. 30. 14:26

본문

728x90
반응형

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;
}
}
}
view raw FileUtils.cs hosted with ❤ by GitHub

읽기 전용 디렉토리 삭제하는 방법입니다.


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 + " 에 폴더가 없습니다.");
}
}
view raw Form1.cs hosted with ❤ by GitHub

메시지 박스 사용하는 방법 입니다.


MessageBox.Show("출력문자") Show 메소드 파라미터에 원하는 문자열을 사용 하면 됩니다.


풀 소스는 아래에 있습니다.

https://github.com/cepiloth/LearningCSharp


728x90
반응형

관련글 더보기

댓글 영역