Hey guys, well I'm bored, and this is a nice and easy tutorial which is actually useful, I use this method quite a lot actually.
intro
File input and output may seem hard to the average VB6 user trying to switch to Visual Basic 2005. This is because the functions for I/O vary between the two languages. Even though VB 2005 may seem like an enhancement upon VB6, it is a complete remake of the language. File I/O will require you to import some classes.
The StreamReader/Writer classes
Add this to the very top of the code, before it says "Class name":
This gives us easy access to the StreamReader and StreamWriter classes. Using these classes gives you the power of file I/O.
File Input
input is very much easier in VB 2005 than in VB6 IMO, all you have to do is import the classes, declare a variable, and use that variable. So now that we've imported these classes, lets create a ReadFile function:
As you can see, this is really quite simple. The function ReadFile takes its path parameter, and declares srFile as a new StreamReader with that path. Then all we do is assign ReadFile the text inside the file with srFile.ReadToEnd. File input couldn't be easier.
File Output
Output, like input, is very much easier than VB6. Again, we declare a variable, but instead we declare it as StreamWriter instead of StreamReader. After we write to the file, we must flush and close the StreamWriter to finish the process.
In this block of code, pay special attention to srFile.Write(Content). There is another function very similar called WriteLine. Write will write the content supplied and nothing else, which is very useful for printing separate characters. WriteLine will output a LineFeed at the end of the file.
After we write, we flush and then close the StreamWriter. If you take srFile.Close() out, it will then appear as though nothing happens. For an example, take that part of code out and call the sub, then check the file. Nothing will appear to be written!
Appending Text
Ok, so you may have noticed this already if you've fiddled around with the previous sub, that it will overwrite any data stored in the file you want to write to. There's a way around this, however, We are still using StreamWriter.
Here is the variable declaration:
and here is a full appending sub for your benefit:
The End
Hope this helps ... rating please?
intro
File input and output may seem hard to the average VB6 user trying to switch to Visual Basic 2005. This is because the functions for I/O vary between the two languages. Even though VB 2005 may seem like an enhancement upon VB6, it is a complete remake of the language. File I/O will require you to import some classes.
The StreamReader/Writer classes
Add this to the very top of the code, before it says "Class name":
Code:
Imports System
Imports System.IO
This gives us easy access to the StreamReader and StreamWriter classes. Using these classes gives you the power of file I/O.
File Input
input is very much easier in VB 2005 than in VB6 IMO, all you have to do is import the classes, declare a variable, and use that variable. So now that we've imported these classes, lets create a ReadFile function:
Code:
Function ReadFile(Path As String) As String
Dim srFile As StreamReader = New StreamReader(Path)
ReadFile = srFile.ReadToEnd
End Function
As you can see, this is really quite simple. The function ReadFile takes its path parameter, and declares srFile as a new StreamReader with that path. Then all we do is assign ReadFile the text inside the file with srFile.ReadToEnd. File input couldn't be easier.
File Output
Output, like input, is very much easier than VB6. Again, we declare a variable, but instead we declare it as StreamWriter instead of StreamReader. After we write to the file, we must flush and close the StreamWriter to finish the process.
Code:
Sub WriteFile(Path As String, Content As String)
Dim swFile As StreamWriter = New StreamWriter(Path)
srFile.Write(Content)
srFile.Flush()
srFile.Close()
End Sub
After we write, we flush and then close the StreamWriter. If you take srFile.Close() out, it will then appear as though nothing happens. For an example, take that part of code out and call the sub, then check the file. Nothing will appear to be written!
Appending Text
Ok, so you may have noticed this already if you've fiddled around with the previous sub, that it will overwrite any data stored in the file you want to write to. There's a way around this, however, We are still using StreamWriter.
Here is the variable declaration:
Code:
Dim swFile As StreamWriter = File.AppendText(path)
and here is a full appending sub for your benefit:
Code:
Sub AppendFile(Path As String, Content As String)
Dim swFile As StreamWriter = File.AppendText(path)
swFile.WriteLine(Content)
swFile.Flush()
swFile.Close()
End Sub
The End
Hope this helps ... rating please?

