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
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.