Solved

vb.net - write to same text file

Posted on 2011-09-21
7
270 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
[X]
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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

726 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