简体   繁体   English

如何通过foreach循环获取FileInfo?

[英]How to get FileInfo via a foreach Loop?

Trying to convert some VB to C#... (learning C#, too). 试图将一些VB转换为C#...(也学习C#)。 I have some code that loops through files in a directory and retrieves their file information. 我有一些代码循环遍历目录中的文件并检索其文件信息。 I have this originally in VB, but am trying to learn C#, and the online convertors don't give me code that will pass .net 2.0. 我最初在VB中使用它,但我正在尝试学习C#,而在线转换器不会给我通过.net 2.0的代码。

Here is the error: Type and identifier are both required in a foreach statement 这是错误: Type and identifier are both required in a foreach statement

Here is the code I have: 这是我的代码:

DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
FileInfo[] files = null;
files = dirInfo.GetFiles();

FileInfo f = default(FileInfo);
foreach (f in files) {  ...
}

I tried putting foreach(FileInfo f... but it gives me a different error: A local variable named 'f' cannot be declared in this scope because it would give a different meaning to 'f', which is already used in a 'parent or current' scope to denote something else 我尝试使用foreach(FileInfo f...但是它给了我一个不同的错误: A local variable named 'f' cannot be declared in this scope because it would give a different meaning to 'f', which is already used in a 'parent or current' scope to denote something else

How do I fix it? 我如何解决它?

DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
FileInfo[] files = null;
files = dirInfo.GetFiles();

// I removed the declaration of f here to prevent the name collision.
foreach (FileInfo f in files)
{  ...
}

Here is a simpler version of the code: 这是一个更简单的代码版本:

DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
foreach (FileInfo f in dirInfo.GetFiles())
{
}

You should provide type of variable used inside loop. 您应该提供循环内使用的变量类型。 In your case it will be FileInfo . 在你的情况下,它将是FileInfo But with C# 3.0 or later you can just write var and compiler will infer type for you: 但是使用C#3.0或更高版本,您只需编写var ,编译器将为您推断类型:

foreach (FileInfo f in files) 
{  
   // ...
}

Read more about foreach statement here . 在这里阅读有关foreach声明的更多信息

Complete solution (you don't need to initialize iteration variable and array of files): 完整的解决方案(您不需要初始化迭代变量和文件数组):

DirectoryInfo dir = new DirectoryInfo(currentDir);
foreach (FileInfo file in dir.GetFiles()) 
{
   // use file  
}

Here's where it looks like you're going wrong: 这就是你出错的地方:

FileInfo f = default(FileInfo);
foreach (f in files) {  ...
}

You are defining f outside of the loop, and then attempting to define it within the loop. 您在循环外定义f,然后尝试在循环内定义它。

If you need the default to be f, try this: 如果您需要默认值为f,请尝试以下操作:

FileInfo f = default(FileInfo);
foreach (FileInfo file in files)
    {
         relevant code here
    }

Otherwise delete the statement declaring the variable "f" 否则删除声明变量“f”的语句

This should work: 这应该工作:

        DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
        FileInfo[] files = null;
        files = dirInfo.GetFiles();
        foreach (FileInfo f in files)
        {
        }

Edit: 编辑:

This would be cleaner, in my opinion: 在我看来,这将更清洁:

        foreach (FileInfo f in new DirectoryInfo(currentDir).GetFiles())
            {
            }

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

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