Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

vb.net - write to same text file

Posted on 2011-09-21
7
268 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:XK8ER
7 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36576489
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
 
LVL 1

Author Comment

by:XK8ER
ID: 36576506
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
 
LVL 40
ID: 36576513
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:XK8ER
ID: 36576529
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36576572
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 36576599
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36576618
That boils down to:

            Dim lines() As String = System.IO.File.ReadAllLines(strFilePath)
            System.IO.File.WriteAllLines(strFilePath, lines)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question