我想在C#中测试包含文件路径的字符串是否存在该文件(类似于Perl中的-e
测试或Python中的os.path.exists()
),请问有什么方法可以实现吗?
使用:
File.Exists(path)
MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Edit: 在 System.IO 中。
using System.IO;
if (File.Exists(path))
{
Console.WriteLine("file exists");
}
请提供完整路径作为输入。避免使用相对路径。
return File.Exists(FinalPath);
我使用WinForms,使用File.Exists(string path)的方法如下:
public bool FileExists(string fileName)
{
var workingDirectory = Environment.CurrentDirectory;
var file = $"{workingDirectory}\{fileName}";
return File.Exists(file);
}
文件名 必须 包含扩展名,例如myfile.txt
File.Exists(Path.Combine(_workDir, _file));