EvilZone
Programming and Scripting => .NET Framework => : ande October 04, 2010, 04:59:52 PM
-
Here is a quick and simple way of reading/writing files.
Reading binary file.
Dim FileContent() as byte = my.computer.filesystem.readallbytes("MyFile.bin")
Reading text file.
Dim FileContent() as string = my.computer.filesystem.readalltext("MyFile.txt")
Writing binary file (Overwrite if exist)
Dim DataToBeWritten() as byte
My.computer.filesystem.writeallbytes("MyFile.bin", DataToBeWritten, false)
Writing binary file (Append if exist)
Dim DataToBeWritten() as byte
My.computer.filesystem.writeallbytes("MyFile.bin", DataToBeWritten, true)
Writing text file. (Overwrite if exist)
Dim DataToBeWritten as string
My.computer.filesystem.writealltext("MyFile.txt", DataToBeWritten, false)
Writing text file. (Append if exist)
Dim DataToBeWritten as string
My.computer.filesystem.writealltext("MyFile.txt", DataToBeWritten, true)
-
I usually put it in Try ... Catch, End Try, since it can cause some trouble
-
I usually put it in Try ... Catch, End Try, since it can cause some trouble
Using Try / Catch blocks are really meant as a last resort, as they are really rather terrible coding practices :)
-
Using Try / Catch blocks are really meant as a last resort, as they are really rather terrible coding practices :)
When dealing with file system and things that may cause errors often it is a good idea tho
-
When dealing with file system and things that may cause errors often it is a good idea tho
Always good to write some debug code with it when distributing
-
I'd strongly recommend using the methods under the System.IO namespace instead of using old VB references.
It's as simple as:
File.WriteAllBytes(path, data)
File.WriteAllText(path, stringData)
byte[] data = File.ReadAllBytes(path); //C#
Dim data as Byte() = File.ReadAllBytes(path) 'VB
string stringData = File.ReadAllText(path) //C#
Dim stringData as String = File.ReadAllText(path) 'VB