Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Filter file from directory by using File name using linq?

Tags:

c#

linq

var GetFileByFileName = Directory.GetFiles(SourceFilePath)
                                       .Select(x => new FileInfo(x))
                                       .Where(x => x.Name==SourceFileName)
                                       .Take(1)
                                       .ToArray();

This is my code for fetch file by Specified File name.Here i am using array . In here SOURCEFILENAME is an string variable which having the file name.but it is not working.i could get all files from directory.but i need only one file from directory based on the SOURCEFILENAME.? Please help me..Thank You. .

like image 611
Prabhagaran P Avatar asked Dec 13 '25 14:12

Prabhagaran P


1 Answers

if you want to use the way what you currently used then you have implement be in below mentioned way

 var GetFileByFileName = Directory.GetFiles(@"D:\Re\reactdemo")
                                       .Select(x => new FileInfo(x))
                                       .Where(x => x.Name == "package.json")
                                       .Take(1)
                                       .ToArray();

check your SourcePath should look like what I have hard coded in code and your source file should be with extension

like image 109
Dhaval Patel Avatar answered Dec 15 '25 02:12

Dhaval Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!