How to check if line containing a specific string exist in text file with Linq in C#

5 Answers

0 votes
using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool exist = (from line in File.ReadAllLines("d:\\test.txt")
                          where line == "Text File Line three"
                          select line).Count() > 0;

            if (exist)
                Console.WriteLine("Exist"); // Exist
            else
                Console.WriteLine("NOT Exist"); 
        }
    }
}

/*
run:
   
Exist
 
*/


answered Mar 11, 2015 by avibootz
0 votes
using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool exist = (from line in File.ReadAllLines("d:\\test.txt")
                          where line == "one"
                          select line).Count() > 0;

            if (exist)
                Console.WriteLine("Exist"); 
            else
                Console.WriteLine("NOT Exist"); // NOT Exist
        }
    }
}

/*
run:
   
NOT Exist
 
*/


answered Mar 11, 2015 by avibootz
0 votes
using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool exist = (from line in File.ReadAllLines("d:\\test.txt")
                          where line.Contains("one") // LIKE '%text%'
                          select line).Count() > 0;

            if (exist)
                Console.WriteLine("Exist"); // Exist
            else
                Console.WriteLine("NOT Exist"); 
        }
    }
}

/*
run:
   
Exist
 
*/


answered Mar 11, 2015 by avibootz
0 votes
using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool exist = (from line in File.ReadAllLines("d:\\test.txt")
                          where line.StartsWith("Text") // LIKE 'text%' // case sensitive
                          select line).Count() > 0;

            if (exist)
                Console.WriteLine("Exist"); // Exist
            else
                Console.WriteLine("NOT Exist"); 
        }
    }
}

/*
run:
   
Exist
 
*/


answered Mar 11, 2015 by avibootz
0 votes
using System;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool exist = (from line in File.ReadAllLines("d:\\test.txt")
                          where line.EndsWith("three") // LIKE '%text'
                          select line).Count() > 0;

            if (exist)
                Console.WriteLine("Exist"); // Exist
            else
                Console.WriteLine("NOT Exist"); 
        }
    }
}

/*
run:
   
Exist
 
*/


answered Mar 11, 2015 by avibootz

Related questions

1 answer 216 views
1 answer 168 views
1 answer 204 views
1 answer 137 views
137 views asked Aug 11, 2023 by avibootz
1 answer 146 views
146 views asked Dec 23, 2016 by avibootz
1 answer 199 views
199 views asked Jul 1, 2014 by avibootz
...