Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read a particular word from a text file. For example AAA.txt is my text file and it contains words as shown below
Name   := Dell
Server := Dataserver
User   := Mike

The data i want is Dataserver. How do I get it?
Updated 2-Sep-13 3:49am

will Help you

string strvalue = string.Empty;
using (System.IO.StringReader sr = new StringReader("c:\aaa.txt"))
string[] strdata = sr.ReadToEnd().Split(new char[] { '\n' });
foreach (string item in strdata)
if (item.Contains("Server :="))
strvalue = item.Replace("Server :=","");

Do you really use
Key :=Value
or rather
? In the last case, you can use the Windows API for ini-files.
Otherwise, you have to read the file line by line and parse each line with a regex.
Use the below code to get the specified string in the file.txt
After that you remove the strings upto "=";

string regexMatchString= "Server :=Dataserver";
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\Textfile.txt");
string matchedString = Regex.Match(text, regexMatchStrig).ToString();
Rahul Krishnan R 2-Sep-13 8:29am
I need the text which will be in the place of Dataserver. Dataserver is just an example. It can be any text or numbers(IP address).
Literally, I need the value after "Server :=" which can be string or numbers.
Thomas ktg 2-Sep-13 8:33am
Is "Server :=" going to be static or not?
Please find the solution of the same.
Note: I have used the 'Server' as keyword to find the DataServer value.

<blockquote class="FQ"><div class="FQA">Quote:</div>/// <summary>
        /// This method will search for a Key value and return its value
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="referencevalue"></param>
        private static void SearchString(string filePath, string referencevalue, string delimiter)
                if (System.IO.File.Exists(filePath))
                    StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                    string Originaldata = string.Empty;
                    string Updatedata = string.Empty;

                    //Read Line by Line
                    string[] file = System.IO.File.ReadAllLines(filePath);
                    foreach (string line in file)
                        if (line.Contains(referencevalue))
                            string[] arrValue = Regex.Split(line, delimiter);
                            string value = arrValue[1];
                            Console.WriteLine("The Value of Server is " + value);
                    Console.WriteLine("[Error] File does not exists in path -" + filePath);
            catch (Exception ex)

Hope it helps. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900