Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends.

i want to edit line 4 of a textfile in my application startup path.

how can i do that.i searched more but there is solutions for replacing words that are in the all line of textfile.

thanks.
Posted

Basically, you need to read all the file, modify it and then write it all. This is because you modification can change the length of the line, and it means that all the content below will be rewritten due to the shift. In simplest case, you could use:
http://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/bsy4fhsa(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/92e05ft3(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/3det53xh(v=vs.110).aspx[^].

If a file is too big, you will have to create a temporary file and read/write line by line, and, at the end, rename temporary file to the name of original one:
http://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
_Starbug_ 12-Aug-14 5:04am    
i understand your answer. but a problem that i have is-->for example we have a text file with 3 line of text on it.in first line we have "Sony" in second line we have "Sunday" and in third line we have "Sunday" too.and we use first "Sunday" for some work. and second "Sunday" for other thing.and for this time they are same.i want to know how can i just change first "Sunday".

i do not know how to solve this problem.
Sergey Alexandrovich Kryukov 12-Aug-14 11:33am    
Exactly in the way I described above. If some part of is is unclear, please ask a follow-up question. Beside, why not using something more cultured and reliable, such as XML-based persistence (serialization)?
—SA
Alternative approach would be: forget those home-baked manipulations and do something cultured, such as XML-based serialization. The best and (by far) simplest way to do that is Data Contract. Please see: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

You have nothing to do: the serializer stores you data model (whole object graph, if you will) to an XML file or stream and restore it back to memory when needed. Please see my past answers where I advocate this approach:
Creating property files...[^],
How can I utilize XML File streamwriter and reader in my form application?[^].

—SA
 
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