Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have created a Text File (.txt).Whose Structure is as Below

1. qwerty^^saasd^^^adadasd^^
2. asdasdas^^adasdasd^^^asasdasd^^
3. weqweqwew^^^dasdadasd^^^asdasd^^^

it goes on accordingly
I want to pass a text through a Textbox
and want it to append at the last Postion of
First line.

Let the text to be passed is 1234567

so that my new TextFile should be like as below

1. qwerty^^saasd^^^adadasd^^1234567
2. asdasdas^^adasdasd^^^asasdasd^^
3. weqweqwew^^^dasdadasd^^^asdasd^^^

I have tried as

VB
Dim Filepath As String = "E:\F26Q2V1.txt"
        Dim strFileName As String = Filepath

        Dim lines() As String
        lines = File.ReadAllLines(Filepath)
        'Dim cnt As Integer = lines.Count
        'MsgBox(cnt)
        Dim newFile(65535) As String
        newFile(0) = lines(0) & "1234567"
        File.WriteAllLines("E:\F26Q2V1c.txt", newFile)


F26Q2V1.txt is the Original txt file and F26Q2V1c.txt is the result file after appending..
but I am getting the result in F26Q2V1c.txt
as

1. qwerty^^saasd^^^adadasd^^1234567

the rest of the lines are getting deleted
but i want all the lines also.
Help needed
Posted
Updated 6-May-12 20:35pm
v2

There's no need to have another array here.

Simply :

VB
Dim Filepath As String = "E:\F26Q2V1.txt"
Dim strFileName As String = Filepath

Dim lines() As String
lines = File.ReadAllLines(Filepath)
lines(0) = string.Concat(lines(0), "1234567")
File.WriteAllLines("E:\F26Q2V1c.txt", lines)


Or, if you want another array :

VB
Dim Filepath As String = "E:\F26Q2V1.txt"
Dim strFileName As String = Filepath
 
Dim lines() As String
lines = File.ReadAllLines(Filepath)
Dim count As Integer = lines.Count
Dim newFile(count) As String = Array.Copy(lines, 0, count)
newFile(0) = string.Concat(newFile(0), "1234567")
File.WriteAllLines("E:\F26Q2V1c.txt", newFile)
 
Share this answer
 
Comments
Karwa_Vivek 7-May-12 2:54am    
Thanks A Lot..That's What, I was in need Of.Thanks Again
Maciej Los 7-May-12 2:55am    
Good answer, my 5!
 
Share this answer
 
Comments
Karwa_Vivek 7-May-12 2:33am    
No its not InI file.I Just working on .txt
file.
Maciej Los 7-May-12 2:49am    
Ini file is a text file. The only one difference is in extension of file...
In my opinion, the way in which you are trying to achive that is not optimal. Take a look here
Maciej Los 7-May-12 2:54am    
And one more think... If you have found a solution, mark each helpful answer as "solved".

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