4

Trying to convert some VB to C#... (learning C#, too). 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.

Here is the error: 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

How do I fix it?

bgmCoder
  • 6,205
  • 8
  • 58
  • 105

4 Answers4

15
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())
{
}
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • Sorry - I just updated my question to reflect the error I recieve when using that. – bgmCoder May 10 '12 at 21:18
  • Re-read the answer now. I have updated it to make it more clear. – Kendall Frey May 10 '12 at 21:20
  • That was it! I'll mark this as accepted in 4 more minutes. Thanks a bushel. I know I have to read some basics (I know it in VB, but I don't want to translate everything to C#...), but I don't have time to study right now. You all are so helpful! – bgmCoder May 10 '12 at 21:23
  • Why assign `null` to array of files? – Sergey Berezovskiy May 10 '12 at 21:26
  • I tried to avoid modifying the code too much, to keep the OP from becoming confused. – Kendall Frey May 10 '12 at 21:27
  • I don't know about null, but I know that it does what it did in VB code. If not null, what else would be assigned? It is an aspx page, and doesn't know the filelisting until page_load or a submit. – bgmCoder May 10 '12 at 21:31
  • Thanks again! I put that in my page. It looks like that avoids setting `files` to null, too. – bgmCoder May 10 '12 at 21:35
1

You should provide type of variable used inside loop. In your case it will be FileInfo. But with C# 3.0 or later you can just write var and compiler will infer type for you:

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

Read more about foreach statement here.

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  
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • I have to run this under a sharepoint application, hence .net 2.0. using `var` gives up this error: `The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)` – bgmCoder May 10 '12 at 21:20
1

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.

If you need the default to be f, try this:

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

Otherwise delete the statement declaring the variable "f"

Allison Steranko
  • 237
  • 2
  • 11
0

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())
            {
            }
Kevin
  • 7,162
  • 11
  • 46
  • 70