-1

I have this code:

FileInfo finfo = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "backup", file.Key)); 
var fsize = finfo.Length;

if (fsize != file.Value)
{
    DialogResult modifiedcleofiles = MessageBox.Show("Oops! Modified files found! Click OK to move them!", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); 
    if(modifiedcleofiles == DialogResult.OK)
    {
        foreach (FileInfo filemove in finfo)
        {
            finfo.MoveTo(Path.Combine(Directory.GetCurrentDirectory(), "backup", filemove.Name));
        }
     }
     return;
}

But it's error with foreach, how can I fix it?

P.S I'm get this error:

foreach statement does not work with variables of type System.IO.FileInfo

Evaldas L.
  • 323
  • 1
  • 4
  • 16
  • 1
    The error message seems pretty clear to me. You cannot use a foreach statement with a `FileInfo`. What exactly are you trying to do? – default Jul 22 '15 at 14:42
  • The foreach needs an enumerable, you're not passing one you're passing a singular instance of FileInfo. What are you trying to do? You seem to mention files but only seem to operate on a single file? – Lloyd Jul 22 '15 at 14:44

1 Answers1

0

If you would like to iterate multiple items in a directory, you need a DirectoryInfo, not a FileInfo, object.

Assuming that you want to get all files in finfo's directory, the code should be like this:

foreach (FileInfo filemove in finfo.Directory.EnumerateFiles()) {
    ...
}

finfo.Directory gives you DirectoryInfo for finfo's directory, and EnumerateFiles() lets you go over its content in a foreach loop.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523