OpenFileDialog + StreamWriter

Renaud

New member
Joined
Jul 6, 2007
Messages
3
Programming Experience
Beginner
Im doing a program, and at a point, i have to load file, and save the "link" to the file in a txt file so that i may find it the next time i open the program

at first i tried doing:
Dim sWriter As StreamWriter
Dim Wmap As String
Wmap = InputBox("blah blah?")
sWriter = New StreamWriter("..\Wmap.txt")
sWriter.Write(Wmap)
sWriter.Flush()
sWriter.Close()
wich worked perfectly, but since typing the file directory wasnt very user friendly, i changed it to be able to browse a file

Dim sWriter As StreamWriter
Dim Wmap As String
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Wmap = OpenFileDialog1.FileName
sWriter = New StreamWriter("..\Wmap.txt")
sWriter.Write(Wmap)
sWriter.Flush()
sWriter.Close()
End If

wmap saves the right string(tested it with a msgbox) but for some reason i cant write it in a txt anymore -_- as a matter of fact, i cant write ANYTHING in a txt anymore o_O

Any ideal, or thing i did wrong? :confused:
 
Do you know what the Flush function does... I never used that in my streamwriters. And are you trying to append to the file you open or overwrite it? A streamwriter is going to overwrite the text file by default I believe. Take out that flush line and try it, unless you know what it does.
 
yes, i want to overight the file, im honestly not exactly sure what flush does, my teacher just told me "dont try to understand it, just write it every time" xD
im going to try to remove it and test it as soon as i get home :)
 
Yes I have never used the flush command. But based on the fact that you are saying the file is blank. You are writing to the file and that is the last thing you do before you close the stream it would make sense if flush would clear it out. However, your teacher may be correct see what happens if you flush it after you close it. But I have never used to flush command and probably have made hundreds of programs that have used streamwriters.
 
the Flush() method clears all the data from the buffer and writes the data to the file.

.Close() method call's it's own .Flush() method then releases the file back to the system so other programs can now use the file

So if you call .Flush() right before .Close() you're actually calling .Flush() twice.

So to why there's no data in the file after he writes 1 line to it, I can't answer. My suspicion would be in this part: "New StreamWriter("..\Wmap.txt")" because of the "..\" the file is either being created in the current folder's parent (but what is the current folder?) or on the root of the current drive (what's the current drive?)
 
Back
Top