2012-02-18 1 views
1

문서는 말한다 Creates a new file을하고 예외에가 나열File.WriteAllText는 실제로 FileNotFoundException을 던집니까?</p> <pre><code>// Summary: // Creates a new file, writes the specified string to the file, and then closes // the file. If the target file already exists, it is overwritten. </code></pre> <p>첫 번째 줄, 첫 번째 문장 :

// System.IO.FileNotFoundException: 
//  The file specified in path was not found. 

경우에 이런 일이까요? 항상 파일을 생성하면 FileNotFoundException을 throw해서는 안됩니다 ...

설명서가 잘못 되었습니까? 아니면 <remarks> 태그가 누락 되었습니까?

+0

경로의 일부가 없으면 어떻게 될까요? 그것은'FileNotFoundException' 또는'DirectoryNotFound'일까요? –

+0

@JohnSaunders는'DirectoryNotFoundException'입니다 :'{ "경로 'C : \\ ZZZZZZ \\ ZZZ \\ TEST.txt'의 일부를 찾을 수 없습니다."}' – BrunoLM

+2

이것은 복사 - 붙여 넣기 버그입니다. File.ReadAllText() 문서. –

답변

5

File.WriteAllText 결국 호출

private static void InternalWriteAllText(string path, string contents, Encoding encoding) 
{ 
    using (StreamWriter streamWriter = new StreamWriter(path, false, encoding)) 
    { 
     streamWriter.Write(contents); 
    } 
} 

(예외를 던질 수 FileStream 때문에) 이론적으로 InternalWriteAllText 던져 ArgumentException 또는 ArgumentNullException 만에 호출하기 전에 던져 예외의 모든 streamWriter.Write(contents); 잠재적으로 예외를 던질 수있다. 그것이 무엇을하고 어떻게 streamWriter이 열렸는지에 따라 매우있을 법하지 않습니다.

나는 반드시 문서가 이라고 잘못 말할 필요는 없으며, MS가 (매우 드문 경우) 문서화를 통해 엉덩이를 덮고있는 것일 수도 있습니다.

출처 : ILSpy를 사용하여 mscorlib v4.0.0.0 디 컴파일

그냥 mscorlib v2.0.0.0 그것은 (의미는 기본적으로 위의 코드를 직접 변환) 이하 너티 체크를 포함 이외에는 동일한 경우 체크 UPDATE.