C# 使用 Try/Catch 检查文件是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11663978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if file exist with Try/Catch
提问by 3D-kreativ
I'm new to this about Try/Catch. In the code below I have a simple test to check if a file exist. In my task for my C# lesson I must use Try/Catch, and I'm not sure how to use this, should I still use the if statement inside the Try part or is there a better way to do the checking if a file exist inside Try? Is there any difference if the file is a simple txt file or a serialized file?
我是关于 Try/Catch 的新手。在下面的代码中,我有一个简单的测试来检查文件是否存在。在我的 C# 课程任务中,我必须使用 Try/Catch,但我不确定如何使用它,我是否仍应在 Try 部分中使用 if 语句,或者是否有更好的方法来检查文件是否存在里面试试?如果文件是简单的txt文件或序列化文件有什么区别吗?
if (File.Exists("TextFile1.txt"))
{
MessageBox.Show("The file don't exist!", "Problems!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
The Try/Catch way I must use
我必须使用的 Try/Catch 方式
try
{
code to check if file exist here
}
catch
{
error message here
}
采纳答案by Tigran
You already check on the presence of file in your first sniplet. There is no any need, for thiscode to be inside try/catchblock.
您已经检查了第一个 sniplet 中文件的存在。没有任何必要,将此代码放在try/catch块内。
回答by Habib
If you want to check if the file exists without using File.Exist, then you may try opening the file in a try block, and then catching the exception FileNotFoundException.
如果您想在不使用的情况下检查文件是否存在File.Exist,那么您可以尝试在 try 块中打开文件,然后捕获异常FileNotFoundException。
try
{
// Read in non-existent file.
using (StreamReader reader = new StreamReader("TextFile1.txt"))
{
reader.ReadToEnd();
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show("The file don't exist!", "Problems!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// Write error.
Console.WriteLine(ex);
}
回答by Dane Balia
You only use a try/catch, when you have an unexpected error, or you are "expecting" an error from accessing a resource. That sounds a bit confusing, but in your cause, there is neither.
只有在遇到意外错误或“期望”访问资源时出现错误时,才使用 try/catch。这听起来有点令人困惑,但在您的事业中,两者都没有。
If however you opened as stream to READ a file without checking if it exists, that will then be necessary to have a try..catch.
但是,如果您在不检查文件是否存在的情况下以流的形式打开文件以读取文件,则需要尝试 try..catch。
All in all, try..catch's should be applied for safety, and where code is complex/length.
总而言之,try..catch 应该用于安全性,以及代码复杂/长度的地方。
回答by Ria
回答by AksharRoop
try
{
if (!File.Exists("TextFile1.txt"))
throw new FileNotFoundException();
}
catch(FileNotFoundException e)
{
// your message here.
}
回答by SidAhmed
Try this :
尝试这个 :
try
{
if(!File.Exist("FilePath"))
throw new FileNotFoundException();
//The reste of the code
}
catch (FileNotFoundException)
{
MessageBox.Show("The file is not found in the specified location");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
回答by Regfor
Related to this question following thread will be interesting
与此问题相关的以下线程会很有趣
File.Exists API was changed in window 8 and explaination is:
File.Exists API 已在窗口 8 中更改,解释为:
Currently the only way to check if a file exists is to catch the FileNotFoundException. As has been pointed out having an explicit check and the opening is a race condition and as such I don't expect there to be any file exists API's added. I believe the File IO team (I'm not on that team so I don't know for sure but this is what I've heard) is considering having this API return null instead of throwing if the file doesn't exist.
目前检查文件是否存在的唯一方法是捕获 FileNotFoundException。正如已经指出的那样进行显式检查并且打开是竞争条件,因此我不希望添加任何文件存在 API。我相信 File IO 团队(我不在那个团队,所以我不确定,但这是我听说的)正在考虑让这个 API 返回 null 而不是在文件不存在时抛出。
Approach with File.Exists is not thread safe and it was removed from API in windows 8. So just catch FileNotFoundException
使用 File.Exists 的方法不是线程安全的,它已从 Windows 8 的 API 中删除。所以只需捕获 FileNotFoundException

