Solved

Reading and writing files in vb.net

Posted on 2011-03-13
12
170 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 49
vb.net 1 month apart 11 35
VB.Net Determine if a mapped network drive exists 2 30
System.net.Sockets Error 5 26
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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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