Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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
C#
Name   := Dell
Server := Dataserver
User   := Mike

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

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 :=","");
Break;

}
}
 
Share this answer
 
v3
XML
Hi,
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)
        {
            try
            {
                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);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("[Error] File does not exists in path -" + filePath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
</blockquote>

Hope it helps. Thanks.
 
Share this answer
 
Use the below code to get the specified string in the file.txt
After that you remove the strings upto "=";

C#
string regexMatchString= "Server :=Dataserver";
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\Textfile.txt");
string matchedString = Regex.Match(text, regexMatchStrig).ToString();
 
Share this answer
 
v2
Comments
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?
Do you really use
Key :=Value
or rather
key=value
? 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.
 
Share this answer
 

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