Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is equivalent c# code for following statement?

Tags:

c#

linq

var files = from file in  my.Computer.FileSystem.GetFiles(CurDir()) orderby file select file;
like image 894
test_yuuzoo Avatar asked Jun 26 '26 20:06

test_yuuzoo


2 Answers

var files = from file in Directory.GetFiles(Environment.CurrentDirectory)
            orderby file
            select file;

Edit: use the code from Gabe's answer if you're using .Net 4.0; it's better for the reasons he mentions. Use this if you're using 3.5.

like image 92
Michael Petrotta Avatar answered Jun 29 '26 10:06

Michael Petrotta


This is an alternative in newer versions of .Net:

var files = from file in Directory.EnumerateFiles(Environment.CurrentDirectory)
            orderby file
            select file;

Generally speaking, it is preferable to use EnumerateFiles instead of GetFiles because it does not create a whole array of strings before returning. This not only saves the creation of the array, but allows you to start processing as soon as the first filename is read rather than having to wait until the last one is read.

You can think of GetFiles() as EnumerateFiles().ToArray().

like image 23
Gabe Avatar answered Jun 29 '26 11:06

Gabe



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!