Author Topic: Easy read/write file [.NET]  (Read 5212 times)

0 Members and 1 Guest are viewing this topic.

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Easy read/write file [.NET]
« on: October 04, 2010, 04:59:52 pm »
Here is a quick and simple way of reading/writing files.

Reading binary file.
Code: [Select]
Dim FileContent() as byte = my.computer.filesystem.readallbytes("MyFile.bin")

Reading text file.
Code: [Select]
Dim FileContent() as string = my.computer.filesystem.readalltext("MyFile.txt")


Writing binary file (Overwrite if exist)
Code: [Select]
Dim DataToBeWritten() as byte
My.computer.filesystem.writeallbytes("MyFile.bin", DataToBeWritten, false)

Writing binary file (Append if exist)
Code: [Select]
Dim DataToBeWritten() as byte
My.computer.filesystem.writeallbytes("MyFile.bin", DataToBeWritten, true)

Writing text file. (Overwrite if exist)
Code: [Select]
Dim DataToBeWritten as string
My.computer.filesystem.writealltext("MyFile.txt", DataToBeWritten, false)

Writing text file. (Append if exist)
Code: [Select]
Dim DataToBeWritten as string
My.computer.filesystem.writealltext("MyFile.txt", DataToBeWritten, true)
« Last Edit: October 04, 2010, 05:00:20 pm by ande »
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Huntondoom

  • Baron
  • ****
  • Posts: 856
  • Cookies: 17
  • Visual C# programmer
    • View Profile
Re: Easy read/write file [.NET]
« Reply #1 on: December 06, 2010, 09:36:31 pm »
I usually put it in Try ... Catch, End Try, since it can cause some trouble
Aslong as you are connected to the internet, you'll have no privacy

Advanced Internet Search
Clean Up!

Offline Reelix

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: Easy read/write file [.NET]
« Reply #2 on: March 28, 2011, 08:15:48 am »
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 :)

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Easy read/write file [.NET]
« Reply #3 on: March 28, 2011, 11:40:43 am »
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
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Easy read/write file [.NET]
« Reply #4 on: March 28, 2011, 01:20:34 pm »
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
~Factionwars

Offline debug

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 2
    • View Profile
Re: Easy read/write file [.NET]
« Reply #5 on: April 02, 2011, 03:02:40 am »
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
« Last Edit: April 02, 2011, 03:03:21 am by debug »