如何找出文件是否存在于 C#/.NET 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38960/
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
How to find out if a file exists in C# / .NET?
提问by Daren Thomas
I would like to test a string containing a path to a file for existence of that file (something like the -e
test in Perl or the os.path.exists()
in Python) in C#.
我想在 C# 中测试一个包含文件路径的字符串是否存在该文件(类似于-e
Perl 中的测试或os.path.exists()
Python 中的测试)。
采纳答案by Daniel Jennings
Use:
用:
File.Exists(path)
MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
MSDN:http: //msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Edit: In System.IO
编辑:在 System.IO
回答by Peter Hoffmann
using System.IO;
if (File.Exists(path))
{
Console.WriteLine("file exists");
}
回答by shivi
Give full path as input. Avoid relative paths.
提供完整路径作为输入。避免相对路径。
return File.Exists(FinalPath);