Solved

vb.net - write to same text file

Posted on 2011-09-21
7
264 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now