Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Some Text written in my text file:-

ABC Licence Version 1.0
Validated Days=10
MAC Address=xxxxxxxxxxxx

How to read particular text in text file using c# .net?

I have to read only this text xxxxxxxxxxxx on button click event.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

1 solution

Suppose your file is at dekstop with name test.txt

C#
System.IO.StreamReader streamReader = new System.IO.StreamReader("C:\\Users\\PuneetGoel\\Desktop\\test.txt");
        string text = streamReader.ReadToEnd();
        string macAddress = text.Split('=')[2].ToString();
        streamReader.Close();
 
Share this answer
 
Comments
[no name] 9-Apr-14 6:20am    
I have problem with this code.
during compare system macAddress.
When i debugging our program so, it displayed in a label "xxxxxxxxxxxx\r\n\r\n\r\n" my macAddress it's not comparing.
Please help me.
Er. Puneet Goel 9-Apr-14 6:24am    
This depend on what you have saved in the text file. In my text.txt i have saved the folowing lines.
ABC Licence Version 1.0
Validated Days=10
MAC Address=xxxxxxxxxxxx

So if you have something other that this, let me know. Coz this text is very important to be same.
[no name] 9-Apr-14 6:30am    
I am displaying mac address in a label,
It's showing xxxxxxxxxxxx in label but when i compare this macAddress with original system mac addres so, now it,s having problem.
It's not matching my original mac address.
[no name] 9-Apr-14 6:32am    
I am using this code for create text file.

FileStream fs1 = new FileStream("D:\\Licence.DAT", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.Write("ABC Licence Version 1.0");
writer.Write("\n" + "Validated Days=" + "" + txtValidatedDays.Text);
writer.WriteLine("\n" + "MAC Address=" + "" + txtMacAddress.Text);
writer.Close();
Er. Puneet Goel 9-Apr-14 6:39am    
Use This:

System.IO.StreamReader streamReader = new System.IO.StreamReader("D:\\Licence.DAT");
string text = streamReader.ReadToEnd();
string macAddress = text.Split('=')[2].ToString();
macAddress = macAddress.Substring(0, 13);
streamReader.Close();

Note: 0, 13 means mac address length is 14(12 + 2) digit
exp: 0123.4567.89ab

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