如何找出文件是否存在于 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:46:20  来源:igfitidea点击:

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 -etest in Perl or the os.path.exists()in Python) in C#.

我想在 C# 中测试一个包含文件路径的字符串是否存在该文件(类似于-ePerl 中的测试或os.path.exists()Python 中的测试)。

采纳答案by Daniel Jennings

回答by pirho

System.IO.File.Exists(path)

System.IO.File.Exists(路径)

msdn

msdn

回答by Peter Hoffmann

System.IO.File:

System.IO.File:

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);