4
Answers

Looping through an array

Steve DeYoung

Steve DeYoung

7y
300
1
I am trying to loop through an array. The array is filled with names from a text file. If the name isn't in the array I want to skip it and continue looping. This is what I have so far.. The problem i'm having is when the name doesn't exist the loop ends and falls into the catch.  
 
try
{
string[] file = File.ReadAllLines(@"C:\Users\Steve\Documents\Visual Studio 2015\Projects\FileCopier\FileCopier\File.txt");
foreach(string folder in file)
{
if (file.Contains(folder))
{
Directory.Move(Path.Combine(sourceDirectory, folder), Path.Combine(destinationDirectory, folder));
}
else
{
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
 
Answers (4)