How to search string in a file?

Hi,

I would like to know how to search string from a file.

If my filename is "a.txt" and my search string is "abc", how can I do a search for this string If this string return true, I will take some action.

Please advise.

Thanks



Answer this question

How to search string in a file?

  • Paul Menefee

    You would use the ReadToEnd method from the StreamReader class and not from the File class.

    If you are using System.IO reference then the StreamReader class is also included.

    Hopefully this will help you some.



  • dave5631

    Hi,

    Have you tried this code

    string searchString = "abc";
    FileStream fs = new FileStream("a.txt", FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(fs);
    string fileText = reader.ReadToEnd();
    if (fileText.IndexOf(searchString) >= 0)
    {
    // found
    }



  • Gokul Krish

    Hi,

    You can try using the StreamReader's ReadToEnd method. This returns a string where you can use the IndexOf method to check if your search string exists on the file. Try the code below for reference...

    string searchString = "abc";
    FileStream fs = new FileStream("a.txt", FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(fs);
    string fileText = reader.ReadToEnd();
    if (fileText.IndexOf(searchString) >= 0)
    {
    // found
    }


    HTH,


  • JV

    hi

    read the file and store in a variable then search the specified text u want


  • bcshivley

    The path you read and the path you use to write are different. What is going wrong exactly Have you stepped through the code



  • T Evans

    Hi there, I was looking for a similar solution to this problem when I stumbled upon the post. My problem however is slightly different but I believe it can be slightly manipulated to suit my needs.

    Here is an example of my text file:

    Sasko Sam Seedloaf 800gr [6.86] #3##
    Elite Gouda Cheese 500gr [32.90] #5##
    Wholegrain [5.00] #6##
    Wholewheat [6.00] #7##
    Coke [7.00] #0##

    If i break it down, using the first line as an example, each line contains:

    A product: "Sasko Sam Seedloaf 800gr"

    A price: "6.86"

    A quantity "3".

    I am using a groupbox, with all the product names loaded into it so I have the string to search for. I want to modify the quantity value.

    So for example, I need to be able to search through the text file, to find "Wholegrain", which in this case is on the 3rd line. And change the quantity value from '6' to '4'. Any ideas on how to do this

    I have so far,

    StreamReader^ din = File::OpenText(file_path); // Where file path is of course the location of the text file

    String^ all_text = din->ReadToEnd(); // Reads all text into the all_text string variable

    int place_holder = all_text->IndexOf(products_in_stock->Text) // the position of the 'products_in_stock' variable


  • Alias_aix_13

    Hi,

    My code is as follow:

    I had tried ReadToEnd, but I can't find ReadToEnd at System.IO.File.

    Can I used ReadAllText

    If not , can anyone please help.

    I had tried ReadAllText, it seems that it can't work.

    My purpose is to search a string in a file. If it is found, I just close the file. If not , I will append the file with a new string. Please help. Thanks

    try

    {

    string sFile = System.IO.File.ReadAllText("abc123.csv");

    if (sFile.IndexOf("abc") >= 0)

    {

    // show message box

    //found

    }

    else{

    file = new FileStream(ftpFolder + "\\abc123.csv", FileMode.Append, FileAccess.Write);

    StreamWriter sw1 = new StreamWriter(file);

    sw1.WriteLine("def");

    sw1.Close();

    }


  • Josh Brown - MSFT

    Specifically, something like

    string sFile = System.IO.File.ReadToEnd(@"c:\a.txt");

    if (sFile.IndexOf("abc") > -1)

    {

    MessageBox.Show("Presto !!!");

    }



  • How to search string in a file?