Need Help with Streamwriter

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hi guys/girlsl, i need help with stream writer, i can get it read lines and stuff like that but now i need Streamwriter to write to a colour.txt file (already populated with data) and insert a line that isn't already in the file, into alph order eg

combobox reads and displays list from txt file.

their is a colour thats isn't in the text file, so i would like it to take the colour from the combobox that was typed in by the user and place it into the colour.txt file in alphabetical order

(these are car colours so their are 100's and 1000's per manufacturer, its not as basic as i want red blue black lol)

i was able to find loads of data about streamreader but not so much about stream writer sorry guys as i could huess that this is a pretty basic question
 
Easier option is to read all lines, sort them, then write them. Example:
VB.NET:
Dim path = "color.txt"
Dim lines As New List(Of String)(IO.File.ReadAllLines(path))
lines.Add("PaleTurquoise")
lines.Sort()
IO.File.WriteAllLines(path, lines.ToArray)
 
thanks for the reply atleast this time i understand whats going on here :D,
my only problem here is if i use this code it will end up adding the colours to the list every time.
thanks for the sort code though, i could put that in a funtion to soft the list before it puts the colours into the combobox, but is their anyway for it to check if the colour is already their. il be honest my knowledge of vb isn't great, i havn'y really used any oop yet :S and kind of dreading it.

ps: would you know of any good tutorial sites? i have about 3 or 4 but they go into quit small things like loops and funtions, by ref by va, not come across anything really usfull
 
JohnH's code reads the entire file into a List(Of String). That List has a Contains method, which will tell you if the List already contains a particular value. If it does, don't add it again.
 
thanks got it working dont know why i didn't think of the contains method, i was thinging more alog the lines of a match collection :S lol cherz again you two
 
Back
Top