FileNotFoundException in File.AppendAllText

不羁岁月 提交于 2021-02-07 07:14:46

问题


I was reading File.AppendAllText method from msdn

http://msdn.microsoft.com/en-us/library/ms143356.aspx

I saw they have listed FileNotFoundException to possible exceptions list for the method but according to there description about method on the same page

Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

this part

If the file does not exist, this method creates a file

it seems this exception can never be thrown for this method.


回答1:


If you take a look at (say) Mono's implementation (which is compatible with Microsoft's), all File.AppendAllText ends up doing (argument checks aside) is creating a StreamWriter with your path and calling a Write on that object.

Neither the used constructor nor the method are listed anywhere to throw a FileNotFoundException, as far as I can tell, so it might be a documentation error.




回答2:


This thread may be old, but the correct answer given above by One-man-crew was ignored by others. The fact is that the FileNotFoundException is not a typo error. It exists, and I get it. The reason is in the exception message as follows: "Could not find part of the path". As described, the File.AppendAllText will open the file if it does not exist, but it must have a valid path.



来源:https://stackoverflow.com/questions/14626728/filenotfoundexception-in-file-appendalltext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!