我想在c#中测试包含文件路径的字符串是否存在该文件(类似Perl中的-e测试或Python中的os.path.exists())。
当前回答
File.Exists(Path.Combine(_workDir, _file));
其他回答
File.Exists(Path.Combine(_workDir, _file));
我使用WinForms和我使用文件的方式。Exists(string path)是下一个:
public bool FileExists(string fileName)
{
var workingDirectory = Environment.CurrentDirectory;
var file = $"{workingDirectory}\{fileName}";
return File.Exists(file);
}
文件名必须包含像myfile.txt这样的扩展名
给出完整路径作为输入。避免相对路径。
return File.Exists(FinalPath);
System.IO.File:
using System.IO;
if (File.Exists(path))
{
Console.WriteLine("file exists");
}
System.IO.File.Exists(路径)
msdn
推荐文章
- .NET中的KeyDown和KeyPress有什么区别?
- 返回匿名类型的结果?
- 你能解释一下流的概念吗?
- 何时使用IList,何时使用List
- ConfigurationManager。AppSettings在.NET Core 2.0中可用?
- 在c#的控制台应用程序中使用'async
- 在单元测试中设置HttpContext.Current.Session
- 如何开始开发Internet Explorer扩展?
- 更新行,如果它存在,否则插入逻辑实体框架
- 在什么情况下SqlConnection会自动被征召到环境事务范围事务中?
- 用c#解析JSON
- Windows窗体中的标签的换行
- 为什么在c#中使用finally ?
- 为什么不是字符串。空一个常数?
- 为什么我不能在c#中有抽象静态方法?