Solved

Reading and writing files in vb.net

Posted on 2011-03-13
12
167 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
  • 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 74

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

18 Experts available now in Live!

Get 1:1 Help Now