How to check if a file exists in a folder?

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

    It can be improved like so:

    if(Directory.EnumerateFileSystemEntries(ProcessingDirectory, "*.xml").ToList<string>().Count == 0)
        log.Info("no files present")
    

    Alternatively:

    log.Info(Directory.EnumerateFileSystemEntries(ProcessingDirectory, "*.xml").ToList<string>().Count + " file(s) present");
    
    0 讨论(0)
  • 2020-11-29 00:34

    This way we can check for an existing file in a particular folder:

     string curFile = @"c:\temp\test.txt";  //Your path
     Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
    
    0 讨论(0)
  • 2020-11-29 00:34

    This woked for me.

    file_browse_path=C:\Users\Gunjan\Desktop\New folder\100x25Barcode.prn
      String path = @"" + file_browse_path.Text;
    
      if (!File.Exists(path))
                 {
          MessageBox.Show("File not exits. Please enter valid path for the file.");
                    return;
                 }
    
    0 讨论(0)
  • 2020-11-29 00:37

    To check file exists or not you can use

    System.IO.File.Exists(path)
    
    0 讨论(0)
  • 2020-11-29 00:39
    if (File.Exists(localUploadDirectory + "/" + fileName))
    {                        
        `Your code here`
    }
    
    0 讨论(0)
  • 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.");
    
    0 讨论(0)
提交回复
热议问题