vb.net - write to same text file

hello there,
I have this function that works great but im trying to make changes,
instead of creating a new file I would like to save the data to the input file.
how can I do that?
Option Explicit On
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call TextUnixToWin("c:\unix.txt")
    End Sub

    Private Sub TextUnixToWin(ByVal strFilePath As String)
        Try
            Using inFile As New IO.StreamReader(strFilePath)
                Try
                    Using outFile As New IO.StreamWriter(IO.File.Open(strFilePath & ".new", IO.FileMode.CreateNew))
                        Dim buffer As String = String.Empty
                        Try
                            While Not inFile.EndOfStream
                                buffer = inFile.ReadLine()
                                outFile.WriteLine(buffer)
                            End While
                        Catch ex As Exception
                            Debug.Print("Error performing conversion: {0}", ex.Message)
                            Debug.Print("Press any key to exit...{0}", vbCrLf)
                        End Try
                    End Using
                Catch ex As Exception
                    Debug.Print("Could not open out file: {0}", ex.Message)
                    Debug.Print("Press any key to exit...{0}", vbCrLf)
                End Try
            End Using
        Catch ex As Exception
            Debug.Print("Could not open in file: {0}", ex.Message)
            Debug.Print("Press any key to exit...{0}", vbCrLf)
        End Try
    End Sub
End Class

Open in new window

LVL 1
XK8ERAsked:
Who is Participating?
 
nepaluzConnect With a Mentor Commented:
here goes ..
    Private Sub TextUnixToWin(ByVal strFilePath As String)
        Try
            Dim outFile As New List(Of String)
            Using inFile As New IO.StreamReader(strFilePath)
                Try
                    Dim buffer As String = String.Empty
                        Try
                            While Not inFile.EndOfStream
                            buffer = inFile.ReadLine()
                            outFile.Add(buffer)
                        End While
                        File.WriteAllLines(strFilePath, outFile)
                        Catch ex As Exception
                            Debug.Print("Error performing conversion: {0}", ex.Message)
                            Debug.Print("Press any key to exit...{0}", vbCrLf)
                        End Try
                Catch ex As Exception
                    Debug.Print("Could not open out file: {0}", ex.Message)
                    Debug.Print("Press any key to exit...{0}", vbCrLf)
                End Try
            End Using
        Catch ex As Exception
            Debug.Print("Could not open in file: {0}", ex.Message)
            Debug.Print("Press any key to exit...{0}", vbCrLf)
        End Try
    End Sub

Open in new window

0
 
XK8ERAuthor Commented:
this is what I get.

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
Error performing conversion: The process cannot access the file 'c:\unix.txt' because it is being used by another process.
Press any key to exit...
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Trying to read and write at the same time in the same file makes things a lot more complicated. Worst, if a problem arises, you end up with a corrupted file.

It is thus both a lot easier to code and also safer to read in one file, save in a new one, then delete the original one (or keep it with a backup extension) and rename the newly created file to the first one name.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
XK8ERAuthor Commented:
well I am trying to convert about 3500 files and its going to be a paint to rename everything. that's why im trying to code something..
0
 
nepaluzCommented:
Sorry, the line
File.WriteAllLines(strFilePath, outFile)
has to be outside the loop. Remove it and place it after line 21 in my code suggestion
0
 
nepaluzCommented:
I have reproduced  the entire code below and added a check for data before attempting to write the data to file.
    Private Sub TextUnixToWin(ByVal strFilePath As String)
        Try
            Dim outFile As New List(Of String)
            Using inFile As New IO.StreamReader(strFilePath)
                Try
                    Dim buffer As String = String.Empty
                    Try
                        While Not inFile.EndOfStream
                            buffer = inFile.ReadLine()
                            outFile.Add(buffer)
                        End While
                    Catch ex As Exception
                        Debug.Print("Error performing conversion: {0}", ex.Message)
                        Debug.Print("Press any key to exit...{0}", vbCrLf)
                    End Try
                Catch ex As Exception
                    Debug.Print("Could not open out file: {0}", ex.Message)
                    Debug.Print("Press any key to exit...{0}", vbCrLf)
                End Try
            End Using
            If outFile.Count > 0 Then File.WriteAllLines(strFilePath, outFile)
        Catch ex As Exception
            Debug.Print("Could not open in file: {0}", ex.Message)
            Debug.Print("Press any key to exit...{0}", vbCrLf)
        End Try
    End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That boils down to:

            Dim lines() As String = System.IO.File.ReadAllLines(strFilePath)
            System.IO.File.WriteAllLines(strFilePath, lines)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.