Thread: Settings?
View Single Post
Old Sep 15th, 2005, 5:16 PM   #11
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
I ended up doing this how I didnt want to do it. I had to open the file stream and read stream, read through the file and generate a string containing the text that should be written. Close both strings, reopen the file stream, and start a write stream, this time with the mode being Create, so it deletes the old file and creates a new, then writes the string to the file, then closes it... It was the only way I could figure out that works... any better ideas?

        If System.IO.Directory.Exists(txtOpenFileDirectory.Text) Or txtOpenFileDirectory.Text = "" Then
            ' First Create the new text to write to the file
            Dim oFileStream As New System.IO.FileStream("elementalEditor.dat", IO.FileMode.Open)
            Dim oStreamReader As New System.IO.StreamReader(oFileStream)
            Dim iCount As Integer = 0
            Dim strEditFileText As String
            oStreamReader.BaseStream.Seek(0, IO.SeekOrigin.Begin)

            While oStreamReader.Peek <> -1
                ' Line 1 is the default open directory. 
                If iCount = 0 Then
                    strEditFileText = strEditFileText & txtOpenFileDirectory.Text & vbNewLine
                    oStreamReader.ReadLine()
                Else
                    strEditFileText = strEditFileText & oStreamReader.ReadLine() & vbNewLine
                End If
                iCount = iCount + 1
            End While

            oStreamReader.Close()
            oFileStream.Close()

            'Second reopen the stream and write the data
            oFileStream = Nothing
            oFileStream = New System.IO.FileStream("elementalEditor.dat", IO.FileMode.Create)
            Dim oStreamWriter As New System.IO.StreamWriter(oFileStream)
            oStreamWriter.Write(strEditFileText)
            oStreamWriter.Flush()
            oStreamWriter.Close()
            oFileStream.Close()
        Else
            ' Doesnt exist
            MsgBox("The default file open directory does not exist.", MsgBoxStyle.Exclamation)
        End If
brokenhope is offline   Reply With Quote