How to check if a file exists in a folder?

前端 未结 9 1160
暗喜
暗喜 2020-11-28 23:58

I need to check if an xml file exists in the folder.

DirectoryInfo di = new DirectoryInfo(ProcessingDirectory);
FileInfo[] TXTFiles = di.GetFiles(\"*.xml\");         


        
9条回答
  •  时光取名叫无心
    2020-11-29 00:40

    Use FileInfo.Exists Property:

    DirectoryInfo di = new DirectoryInfo(ProcessingDirectory);
    FileInfo[] TXTFiles = di.GetFiles("*.xml");
    if (TXTFiles.Length == 0)
    {
        log.Info("no files present")
    }
    foreach (var fi in TXTFiles)
        log.Info(fi.Exists);
    

    or File.Exists Method:

    string curFile = @"c:\temp\test.txt";
    Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
    

提交回复
热议问题