如何通过现有的文件,以检测System.IO.IOException原因是什么? [英] How to detect System.IO.IOException cause by existing file?

查看:1975
本文介绍了如何通过现有的文件,以检测System.IO.IOException原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建和打开一个文件,但只有当犯规存在的话。我不想使用File.Exists,因为它创建具有相同名称的文件后,切换线程。

I want to create and open a file but only if it doesnt exist. I dont want to use a File.Exists because a thread by switch after it creating a file with the same name.

我如何检查是否异常System.IO。 IOException异常是由现有的文件引起的?我不喜欢解析错误味精(即使寿它可以像.indexOf简单(存在))

How do i check if the exception System.IO.IOException was caused by the file existing? I prefer not to parse the error msg (even tho it can be as simple as .indexOf("exist"))

我应该怎么做呢?

推荐答案

您应该避免以往任何时候都使得基础上,Exception.Message属性的内容逻辑的决定。被定义为人类可读的消息时,不是对一个程序来读取。这些消息如有更改,恕不另行通知。

You should avoid ever making logic decisions based on the contents of the Exception.Message property. That is defined to be a human-readable message, not something for a program to read. Such messages are subject to change without notice.

可读信息经常改变,使他们更容易阅读到人类。这不会使你的程序更幸福。

Human-readable messages often change to make them more readable to humans. That won't make your program any happier.


正如你可能能够告诉,这取决于人类可读的消息程序逻辑的问题是我的忌讳 - 我不会告诉你多久,因为这会让我觉得自己老了。这已经心烦意乱我从东西应该是很明显的。

As you might be able to tell, the issue of program logic depending on human-readable messages is a pet peeve of mine - I won't tell you for how long, since that would make me feel old. This has distracted me from something that should have been obvious.

请检查一下,如果你得到一个的当文件不存在System.IO.FileNotFoundException 。尝试追赶,与其IOException异常。

Please check to see if you get a System.IO.FileNotFoundException when the file does not exist. Try catching that instead of IOException.

这篇关于如何通过现有的文件,以检测System.IO.IOException原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆