Error with Directory.GetFiles

Hey, I’m getting a strange error with a fairly straightforward GetFiles method - it seems like for some reason the method confuses the boundaries between folder Path and search pattern. Please take a look:

the error that I get is:


It seems like it doesn’t identify the comma separating arguments correctly, and includes part of the search pattern in the folder path. Why is that? Appreciate all help


Write comma before “*.pdf” instead of plus.



Hi @salladinne ,

You can check the below steps and resolve the issue :

  1. Check whether such a path exists
    2.Can add the folder path to a variable and use it as shown below:


It will create three arguments (two commas) in total - the method only accepts two, so I get a compliation error

Doesn’t seem to be related to the slash - I get similar error on other invoices which don’t have slash

Are you sure that your filenames contain slash? Slash is usually an illegal character in filenames in Windows/Linus/MacOS since it’s being used as directory separator.


We can use either of the one here to read pdf files from folder.

Find pdf file from parent and it’s sub folders.

      Directory.GetFiles("Folder Path","*.pdf", SearchOption.AllDirectories)

Find pdf file from specific folder.

      Directory.GetFiles("Folder Path","*.pdf")

But pdf doesn’t solve the search pattern, I need other variables there - so
Directory.GetFiles(“Folder Path”,"
.pdf") is not enough,
It also doesn’t explain why the Method takes whatever is after the comma (being second argument) as path folder (first argument)

Yes, analyzed it once again and it’s definitely the slash - must have influenced the Directory.GetFiles that it instead of giving out message “illegal character in search pattern”, it twisted into thinking that name of the file is part of the folder name itself.

Thanks for help guys, I will mark the solution

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.