Solved

vb.net - write to same text file

Posted on 2011-09-21
7
267 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView does not show data rows correctly in vb.net 2013 3 39
VB.Net - Windows UserName index and length error 4 29
Crystal reports vb.net 2 40
Expression Evaluater 3 24
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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