디렉토리를 삭제하려면 먼저 파일을 모두 삭제해야한다는 것을 알고 있습니다.디렉토리 내의 디렉토리 삭제
그러나 빈 서브 디렉토리가있는 디렉토리를 삭제하려면 먼저 해당 서브 디렉토리를 삭제해야합니까? 또는 주 디렉토리를 삭제할 수 있습니까?
디렉토리를 삭제하려면 먼저 파일을 모두 삭제해야한다는 것을 알고 있습니다.디렉토리 내의 디렉토리 삭제
그러나 빈 서브 디렉토리가있는 디렉토리를 삭제하려면 먼저 해당 서브 디렉토리를 삭제해야합니까? 또는 주 디렉토리를 삭제할 수 있습니까?
당신은 반복적으로 삭제하려고 할 수 있습니다 : 당신은 적절한 권한이있는 경우
var path = Path.GetFullPath(@"C:\Temp\DeleteMe");
Directory.Delete(path,true); // true for recursive
이 모든 것을 포함 파일을 삭제해야합니다.
Directory.Delete 반복 플래그를 true로 설정하면 작업을 수행해야하며 먼저 비울 필요가 없습니다.
Directory.Delete(path, true);
난 그냥 태그가있는 경우에 당신은 당신이가는대로 모든 파일과 폴더를 열거하고 삭제해야합니다, IsolatedStorage을 의미 것으로 나타났습니다.
How to: Delete Files and Directories in Isolated Storage
어쨌든 삭제할 때 비어 있는지 확인하십시오.
디렉터리에 readonly
파일이 없다는 확신이있는 경우에만 Directory.Delete(yourpath,true)
메서드를 사용할 수 있습니다. 그렇지 않으면 exception
이 표시됩니다. 대신에 파일을 삭제하기 전에 파일을 정상적으로 표시 할이 같은 재귀 적 메서드를 사용할 수 있습니다.
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
솔직히 여기를 물어보고 대답을 기다리는 대신 시도해 보면 정가가 빨라질 것입니다. 테스트 폴더를 사용하여 테스트 프로젝트를 만들고 실행하면 꽤 빨리 볼 수 있습니다. –