Replacing text in a file then making the file.

{IP}Gil-Galad

Member
Joined
Sep 25, 2005
Messages
19
Programming Experience
Beginner
Well ive had this problem for about a month and this is what ive been trying to do

I have a INI. file in my bin folder, i need to search through it and replace all occurences of certain words. Then it must create it in the programs folder like CreateText for StreamWriter. I no this has to be possible and if anyone would post some help it would be greatly appreciated. Im in dire need of this so i can finish this program once and for all.

my Teacher said something like i would have to go through the file and look for certain lines and replace them and have it loop? Im want it my way though that would be alot more efficient for me and alot easier. Thank you for your time.
 
Sorry for the inconvenience but may i ask you why your teacher wants you to use INI file? It seems like he is still living in VB6 (and earlier) era. Microsoft wants people to use XML files as using XML files is the preferred way to store initialization data in .NET (the registry is getting cluttered which makes it slow and INI files are much less flexible than XML files). Btw, wellcome to the forum :)

Regards ;)

P.S. i will answer your original question anyway but, first let me know why your teacher wants you to use INI file, please.
 
no no no im not using ini file this is the deal.

The part of the program im making opens a document (Premade located in the bin folder of course)(its in ini form for game purposes), replaces all occurences of words in it, then it recreates the document in the programs folder. The reason im not just writing the file with streamwriter is because it is 20000 lines and thats alot of coding.
 
You've mentioned a teacher, so I'm going to assume that this is homework and not provide any specific code. If you want to replace text in a file then you MUST read in the file and the re-save it. Note that the StreamReader.ReadToEnd method will read the whole file and return its contents as a String object, and the String.Replace method can be used to make substitutions in the text.
 
Its not an asignment and its done already so this is old I did something like this..

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] StreamReader()
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] OpenFileDialogBox1
[/SIZE][SIZE=2][COLOR=#008000]'All the OpenFileDialog1 stuff here
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.StreamReader = IO.File.OpenText(.Filename)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Line [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] sr.Peek <> -1
Line = sr.ReadLine
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Line.StartsWith("Beginning Words i already no ") [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Line.Replace("The Old Word", "New Word")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE][SIZE=2]MsgBox("Your File has been Editted", MsgBoxStyle.Exclamation, "Attention")
Beep()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
the code that you posted there, doesnt replace anything

it reads the file, line by line and on each line it checks what the line starts with and if it does then it replaces that line

but before you write the changed line back to the file you just read the next line which disregards the changes

what you should do instead is read the entire file into a string (use sr.ReadToEnd) then use the Split function to split it by ControlChars.NewLine
then check each line (each element in the array) to see if it starts with whatever and make the changes
if there are any changes be sure to change that array element to what you've changed
then when you're done, use the StreamWriter and write each element of the array (the file) to a new line in the streamwriter and there ya go, changes are made to the file itself
 
As JB says, a StreamReader just does what its name suggests and reads a stream. If you want to make any changes to the file on disk then you need to use a StreamWriter as well. If you don't want to read the entire file into memory first because of it's size, I suggest you use the IO.Path.GetTempFile method to create a genuine temp file to which you can write the edited results line by line as you read in the file. Once you've finished you can just delete the origianl file and move the temp file to that location.

Finally, may I suggest that you follow the advice offered in my signature and indicate that your thread is resolved if you are happy with the outcome. Otherwise we don't know that you don't need further help.
 
Back
Top