What I have going on is two files, one file contains:
Orange
Apple
The second file contains
Orange~1.txt
Orange~2321.txt
Apple~12.txt
Apple~23.txt
Ap~23.txt
OROR~23.txt
What I need to do is is if the first file matches a line in the second file, copy that file out to a new directory. What I have going on now never finds any matching items.
string[] content = File.ReadAllLines(@"C:\Cact.txt");
string[] mastercontent = File.ReadAllLines(masterdin + "\\Master.txt");
foreach (string con in content)
{
if (mastercontent.Contains(con))
{
File.Copy(masterfolder + "\\" + con, masterdin);
}
}
You’re calling the LINQ
Containsmethod on the array, which will return true if the array contains a string which is exactly equal tocon.The simplest solution is to change
mastercontentto a single string by callingFile.ReadAllText.This will call
String.Contains, and will check whetherconappears anywhere in the large string. (This would behave differently ifconhas newlines, but it can’t)Alternatively, you can use LINQ to check whether
mastercontenthas any strings containingcon, like this:EDIT: To match case-insensitively, you can change it to the following: