Solved

Reading and writing files in vb.net

Posted on 2011-03-13
12
168 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 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

16 Experts available now in Live!

Get 1:1 Help Now