Search code examples
c#directorygetfiles

Find file with Name and Extension


I want to find a file that has .pdf extension and I want to find with name too. For example I have filename = "Work.pdf". I want to write a method that could find with name. Is there any builtin method for this?

string fileName = "Work.pdf"
string[] files = System.IO.Directory.GetFiles("C:\Files", "*.pdf");
// string myWorkPdfFile = files.Where() //search the files with fileName

Solution

  • You can try to pass fileName value be second parameter.

    string fileName = "Work.pdf";
    string[] files = System.IO.Directory.GetFiles(@"C:\Files", fileName);