C# file search

Soldato
Joined
6 Mar 2008
Posts
10,082
Location
Stoke area
Hi,

I had some code that worked but was lost during a rebuild as our service desk didn't back up as they were supposed to, where we run an app, it checks a file for a search term and returns a found or not found result:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace PCIDSSCheck
{
    class Program
    {
        static void Main(string[] args)

        {
            
            // set search string
            string searchText = "Thread:";
            
            // find the latest file in the directory
            var directory = new System.IO.DirectoryInfo(@"\\DRIVE_LOCATION\LogFiles\");

            var myFile = directory.GetFiles()
                         .OrderByDescending(f => f.LastWriteTime)
                         .First();

            // sets the path name to directoty plus the latest file name
            string path = @"\\DRIVE_LOCATION\LogFiles\" + myFile;

            // Open and read the file.
            string[] lines;     //string array

            lines = File.ReadAllLines(path);
            int arrayLength = lines.Length;
            

            // Search for the required text

            var results = Array.Exists(lines, s => s.Equals(searchText));

            // write out the answer

            Console.WriteLine("The following file has been checked at " + DateTime.Now.ToString("h:mm"));
            Console.WriteLine();
            Console.WriteLine(path);
            Console.WriteLine();
            Console.WriteLine("Was the search string found:");
            Console.WriteLine();
            Console.WriteLine(results);
            
            // wait for user interaction to close console screen
            Console.ReadLine();


        }
    }
}

I've opened the file, i know the search term is in there but it's always returning a FALSE. Can anyone spot the more than likely obvious mistake? I know there are no doubt better ways to do this btw :)
 
Back
Top Bottom