How to check if a file exists in a folder?

前端 未结 9 1192
暗喜
暗喜 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:52

    Since nobody said how to check if the file exists AND get the current folder the executable is in (Working Directory):

    if (File.Exists(Directory.GetCurrentDirectory() + @"\YourFile.txt")) {
                    //do stuff
    }
    

    The @"\YourFile.txt" is not case sensitive, that means stuff like @"\YoUrFiLe.txt" and @"\YourFile.TXT" or @"\yOuRfILE.tXt" is interpreted the same.

提交回复
热议问题