Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Writing a file to disk

Writing a file to disk

Scheduled Pinned Locked Moved Visual Basic
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pugman812
    wrote on last edited by
    #1

    I was wondering how you would write a file to disk. In 6.0 you used the open command. I was just wanted to know the process of writing to disk.

    D 1 Reply Last reply
    0
    • P Pugman812

      I was wondering how you would write a file to disk. In 6.0 you used the open command. I was just wanted to know the process of writing to disk.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      What kind of file were you looking at writing? There are a few methods available. Text files are the easiest and can be done using the FileStream classes:

          Dim sr As StreamWriter = File.CreateText(FILE\_NAME)
          sr.WriteLine("This is my file.")
          sr.Close()
      

      Binary files are a little more complicated but are still be based on FileStream classes:

          Dim fs As New FileStream(FILE\_NAME, FileMode.CreateNew)
          ' Create the writer for data.
          Dim w As New BinaryWriter(fs)
      

      ' Write data.
      Dim i As Integer
      For i = 0 To 10
      w.Write(CInt(i))
      Next i
       
      w.Close()
      fs.Close()
       
      ' Create the reader for data.
      fs = New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
      Dim r As New BinaryReader(fs)
      ' Read data from Test.data.
      For i = 0 To 10
      Console.WriteLine(r.ReadInt32())
      Next i
      w.Close()

      For more information, lookup 'FileStream Class' in the VS Help Index, or on MSDN here[^]. RageInTheMachine9532

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups