如何确定C#/ .NET中是否存在文件?

问题描述 投票:163回答:4

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

c# .net io
4个回答
274
投票

使用:

File.Exists(path)

MSDN:http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

编辑:在System.IO中


51
投票

System.IO.File

using System.IO;

if (File.Exists(path)) 
{
    Console.WriteLine("file exists");
} 

25
投票

System.IO.File.Exists(路径)

msdn


4
投票

提供完整路径作为输入。避免相对路径。

 return File.Exists(FinalPath);
© www.soinside.com 2019 - 2024. All rights reserved.