Reading and writing files in vb.net

Anybody know to read and write files in vb.net and creat new files from old files and hence delete them as well etc. What sort if coding is required.
mahmood_786Asked:
Who is Participating?
 
nepaluzConnect With a Mentor Commented:
yep they can. you only have to import the respective libraries.
0
 
nepaluzConnect With a Mentor Commented:
not much code

To read
Dim MyContents = File.ReadAllLines("Pathetofile")

Open in new window

To delete
File.Delete("PathToFile")

Open in new window

0
 
käµfm³d 👽Connect With a Mentor Commented:
There are many approaches you can take in order to do this in VB.NET. Some classes of interest to you would be:

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
mahmood_786Author Commented:
Can these commands be used in in console mode as well fir vb. Net 2005. Any examples will be much appreciated .
0
 
mahmood_786Author Commented:
Can these commands be used in in console mode as well fir vb. Net 2005. Any examples will be much appreciated .
0
 
käµfm³d 👽Commented:
Here is one example:
Imports System.IO

Module Module1

    Sub Main()
        Dim fileContents As String

        Using reader As New StreamReader("filename.txt")
            fileContents = reader.ReadToEnd()
        End Using

        fileContents = String.Concat(fileContents, "Some new text to add")

        Using writer As New StreamWriter("filename.txt", False)
            writer.Write(fileContents)
        End Using
    End Sub

End Module

Open in new window

0
 
Mohamed AbowardaConnect With a Mentor Software EngineerCommented:
First thing you should consider is what kind of file you want to read/write? Binary or Text file

If you want to read/write on binary files, you need to use BinaryReader and BinaryWriter classes:
http://www.dotnetperls.com/binaryreader-vbnet
http://vb.net-informations.com/files/vb.net_binaryWriter.htm

Otherwise, you can use StringReader and StringWriter or TextReader and TextWriter to write string to files:
http://vb.net-informations.com/files/vb.net_TextReader.htm
http://vb.net-informations.com/files/vb.net_TextWriter.htm
0
 
Mohamed AbowardaConnect With a Mentor Software EngineerCommented:
You can also take a look on File class that can be helpful:
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
0
 
mahmood_786Author Commented:
Ok and how I will create the new file using the results of the calculations?
0
 
Mohamed AbowardaSoftware EngineerCommented:
There is simple way to create files, you can use File.Create() method, however I recommend you to use StreamWriter and/or StringWriter or TextWriter.
0
 
mahmood_786Author Commented:
That was extremely useful information, before I give all this a try I need to say that :

I am creating two files , writng them and saing them and then reading them and doing caluclation and saving calcualtions and, all this is done using console mode,. The output is saved a named file and then can be changed as well  using console mode. any more ideas are welcome. Onece i get  a final reply all put all my code here and have a go.
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.