简体   繁体   English

以下语句的等效c#代码是什么?

[英]what is equivalent c# code for following statement?

var files = from file in  my.Computer.FileSystem.GetFiles(CurDir()) orderby file select file;
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; 编辑:如果您使用.Net 4.0,请使用Gabe答案中的代码; it's better for the reasons he mentions. 他提到的原因更好。 Use this if you're using 3.5. 如果您使用3.5,请使用此选项。

This is an alternative in newer versions of .Net: 这是较新版本的.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. 一般来说,最好使用EnumerateFiles而不是GetFiles因为它在返回之前不会创建整个字符串数组。 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() . 您可以将GetFiles()视为EnumerateFiles().ToArray()

I'll do it with the other syntax :) 我会用其他语法:)

var files = System.IO.Directory.EnumerateFiles(Environment.CurrentDirectory)
                               .OrderBy(f => f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM