MSDN 링크: DirectoryNotFoundException 클래스 (System.IO) | Microsoft Docs
DirectoryNotFoundException은 파일이나 디렉터리 이름을 찾을 수 없을 때 발생하는 오류이다.
파일이나 디렉터리 이름을 잘 확인하면 해결할 수 있다.
예시
예시로 아래는 Unity에서 Log를 출력하려고 급조한 스크립트이다.
나중에 보니 Log Viewer라는 좋은 에셋이 있어 분노에 차서 삭제했다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using System;
using System.IO;
using System.Text;
using UnityEngine;
public class DataDebugScript : MonoBehaviour
{
public static string logPath;
void Awake()
{
DateTime nowDate = DateTime.Now;
string directoryPath = $@"{Application.dataPath}\Log";
logPath = Path.Combine(directoryPath, $"Log_{nowDate.ToString("yyyyMM-dd HH:mm:ss")}.txt");;
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
if (!File.Exists(logPath))
File.Create(logPath);
}
public static void PrintLog(string log)
{
File.AppendAllText(logPath, log + "\r\n");
}
}
|
cs |
오류
DirectoryNotFoundException: Could not find a part of the path ...
위의 코드에서는 22번 라인에서 DirectoryNotFoundException 오류가 떴는데, 문제점은 바로 파일 이름에 들어갈 수 없는 문자가 들어간 점이었다.
ko.wikipedia.org/wiki/%ED%8C%8C%EC%9D%BC_%EC%9D%B4%EB%A6%84
파일 이름에는 /, \, ?, %, *, :, |, ", <, >, . 와 같은 특수 문자를 사용할 수 없다고 한다.
제 코드에서 날짜와 시간을 표시하려다가 콜론(:)을 사용하는 바람에 오류가 발생한 것으로 보인다.
13
14
|
string directoryPath = $@"{Application.dataPath}\Log";
logPath = Path.Combine(directoryPath, $"Log_{nowDate.ToString("yyyyMMdd")}.txt");
|
cs |
위와 같이 파일 경로명을 수정해서 해결했다.
정말 기초적인 실수라 당분간은 못 잊을 것 같다.
댓글