Paul Dolan Paul Dolan - 25 days ago 18
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

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.

Comments