Paul Dolan Paul Dolan - 11 months ago 75
C# Question

The name 'file' does not exist in the current context.. Third line

foreach (var file in files)

File.Move(File.FullName, ConvertToUnsign(file.Fullame.ToLower().Replace("'", String.Empty).Replace("-", String.Empty)));


It is giving me an error for 'file' here

lstNames.Add(file.Name.Replace(".jpg", String.Empty));
return lstNames;

Answer Source

You missed the curly braces in your foreach loop:

foreach (var file in files)
{
    File.Move(File.FullName, ConvertToUnsign(file.Fullame.ToLower().
                             Replace("'", String.Empty).Replace("-", String.Empty)));
    lstNames.Add(file.Name.Replace(".jpg", String.Empty));
}
return lstNames;

In any loop structure, if you don´t enclose the code in curly braces {}, the loop only affects to the first line following the loop line. So in this case, the foreach only would affect to the File.Moveline.