Click to See Complete Forum and Search --> : File.ReadAllLines Question


stevetaylor15
August 20th, 2009, 02:53 AM
I have the following that i'm tryingto search one directory and all files with a string value below. The problem is that it keeps returning that it could 'Could not find file 'U:\System.IO.FileInfo[]'.'. The string directory does exist as a valid path. any ideas?

/*

string logdir = (string)Key.GetValue("LogDirectory");

string directory = logdir;
DirectoryInfo dirinfo = new DirectoryInfo(directory);
System.IO.FileInfo[] files = dirinfo.GetFiles("*.txt");

foreach (FileInfo fi in files)
{
sw.WriteLine(fi.Name, fi.Extension);

foreach (string line in File.ReadAllLines(files.ToString()))
{
if (line.Contains("crap"))
sw.WriteLine(fi + "contains" + line.ToString() + Environment.NewLine);
}

}
/*

dannystommen
August 20th, 2009, 04:06 AM
The message looks clear to me, the filename does not exists.

your error

string line in File.ReadAllLines(files.ToString())


change it to

string line in File.ReadAllLines(fi.FullName)

rliq
August 20th, 2009, 04:47 AM
Yes, don't assume (and I have before on many occasions) that you know what "someObject.ToString();" will return. As Danny has correctly pointed out..

stevetaylor15
August 20th, 2009, 06:05 AM
go i'm such an idiot. i did use FullName but like a method for some stupid reason!

Thank guys..yea i know it was a stupid question!

rliq
August 20th, 2009, 08:14 PM
go i'm such an idiot. Remember Kaa's Law. In a group of N people, most are idiots. For sufficiently large vales of N. ;)