Solved

Reading and writing files in vb.net

Posted on 2011-03-13
12
171 Views
Last Modified: 2012-12-28
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.
0
Comment
Question by:mahmood_786
[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
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 200 total points
ID: 35122455
not much code

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

Open in new window

To delete
File.Delete("PathToFile")

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 35122463
There are many approaches you can take in order to do this in VB.NET. Some classes of interest to you would be:

0
 

Author Comment

by:mahmood_786
ID: 35122646
Can these commands be used in in console mode as well fir vb. Net 2005. Any examples will be much appreciated .
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!

 

Author Comment

by:mahmood_786
ID: 35122656
Can these commands be used in in console mode as well fir vb. Net 2005. Any examples will be much appreciated .
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 200 total points
ID: 35122659
yep they can. you only have to import the respective libraries.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35122768
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
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 200 total points
ID: 35124402
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
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 200 total points
ID: 35124411
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
 

Author Comment

by:mahmood_786
ID: 35124584
Ok and how I will create the new file using the results of the calculations?
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35124593
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
 

Author Comment

by:mahmood_786
ID: 35125083
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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