C #의 단일 전체 경로에서 여러 디렉토리를 만드는 방법은 무엇입니까? “C:\dir0\dir1\dir2\dir3\dir4\”모든 디렉토리가 존재하도록 어떻게 구현하는 것이 가장

전체 경로가있는 경우 : "C:\dir0\dir1\dir2\dir3\dir4\"모든 디렉토리가 존재하도록 어떻게 구현하는 것이 가장 좋습니까?

BCL에이를위한 방법이 있습니까? 그렇지 않은 경우 가장 우아한 방법은 무엇입니까?



답변

나는 부를 것이다 Directory.CreateDirectory(@"C:\dir0\dir1\dir2\dir3\dir4\").

일반적인 믿음과 Directory.CreateDirectory는 달리 존재하지 않는 상위 디렉토리를 자동으로 작성합니다.
MSDN의 말로Creates all directories and subdirectories as specified by path.

전체 경로가 이미 존재하면 아무 것도 수행하지 않습니다. (예외는 발생하지 않습니다)


답변

완전한 파일 경로에서 디렉토리 작성

private String EvaluatePath(String path){

    try
    {
        String folder = Path.GetDirectoryName(path);
        if (!Directory.Exists(folder))
        {
            // Try to create the directory.
            DirectoryInfo di = Directory.CreateDirectory(folder);
        }
    }
    catch (IOException ioex)
    {
        Console.WriteLine(ioex.Message);
        return "";
    }
    return path;
}