Write Data To File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SHVzYW0=?=

    Write Data To File

    Hi EveryBody:

    How can I write a byte array to FileStream, I tried to do so and I use the
    following code:
    Dim rawan As New List(Of Byte())
    Dim msg() As Byte = CType(rawan(Tex tBox1.Text), Byte())
    Dim File_Name As String = "Winter.jpg "
    Dim fs As New FileStream(File _Name, FileMode.Create New)
    ' Create the writer for data.
    Dim w As New BinaryWriter(fs )
    ' Write data to Test.data.
    Dim i As Integer
    For i = 0 To msg.Length
    w.Write(CInt(i) )
    Next i
    w.Close()
    fs.Close()

    But I got the following message Can not can't not access file. So how can I
    write my data to file or file stream? any help will be appreciated

    regard's

    Husam
  • \(O\)enone

    #2
    Re: Write Data To File

    Husam wrote:
    How can I write a byte array to FileStream, I tried to do so and I
    use the following code:
    [...]

    If you're using VS2005 or later (you didn't say if you were) and you want to
    write a byte array to a file, this is an easier solution:

    \\\
    IO.File.WriteAl lBytes("Winter. jpg", msg)
    ///

    HTH,

    --

    (O)enone


    Comment

    • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

      #3
      RE: Write Data To File

      (O)enone is right, but in your code you don't have any Open calls on your
      stream or writer. That would be necessary.

      "Husam" wrote:
      Hi EveryBody:
      >
      How can I write a byte array to FileStream, I tried to do so and I use the
      following code:
      Dim rawan As New List(Of Byte())
      Dim msg() As Byte = CType(rawan(Tex tBox1.Text), Byte())
      Dim File_Name As String = "Winter.jpg "
      Dim fs As New FileStream(File _Name, FileMode.Create New)
      ' Create the writer for data.
      Dim w As New BinaryWriter(fs )
      ' Write data to Test.data.
      Dim i As Integer
      For i = 0 To msg.Length
      w.Write(CInt(i) )
      Next i
      w.Close()
      fs.Close()
      >
      But I got the following message Can not can't not access file. So how can I
      write my data to file or file stream? any help will be appreciated
      >
      regard's
      >
      Husam

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Write Data To File

        Husam,

        Can you have a look what happens when you set option Strict to on, I have
        the idea that you want to write nothing.

        Cor

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Write Data To File

          "(O)enone" <oenone@nowhere .comschrieb:
          >How can I write a byte array to FileStream, I tried to do so and I
          >use the following code:
          [...]
          >
          If you're using VS2005 or later (you didn't say if you were) and you want
          to write a byte array to a file, this is an easier solution:
          >
          \\\
          IO.File.WriteAl lBytes("Winter. jpg", msg)
          ///
          .... or 'My.Computer.Fi leSystem.WriteA llBytes', which does pretty much the
          same as the above.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Guru

            #6
            Re: Write Data To File

            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
            news:ueJdSD7WIH A.5816@TK2MSFTN GP06.phx.gbl...
            "(O)enone" <oenone@nowhere .comschrieb:
            >>How can I write a byte array to FileStream, I tried to do so and I
            >>use the following code:
            >[...]
            >>
            >If you're using VS2005 or later (you didn't say if you were) and you want
            >to write a byte array to a file, this is an easier solution:
            >>
            >\\\
            > IO.File.WriteAl lBytes("Winter. jpg", msg)
            >///
            >
            ... or 'My.Computer.Fi leSystem.WriteA llBytes', which does pretty much the
            same as the above.
            What a pair of morons you two are. He's using a BinaryWriter, in case you
            hadn't noticed.




            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Write Data To File

              "Guru" <running@interf erence.nitschri eb:
              >>>How can I write a byte array to FileStream, I tried to do so and I
              >>>use the following code:
              >>[...]
              >>>
              >>If you're using VS2005 or later (you didn't say if you were) and you
              >>want to write a byte array to a file, this is an easier solution:
              >>>
              >>\\\
              >> IO.File.WriteAl lBytes("Winter. jpg", msg)
              >>///
              >>
              >... or 'My.Computer.Fi leSystem.WriteA llBytes', which does pretty much the
              >same as the above.
              >
              What a pair of morons you two are. He's using a BinaryWriter, in case you
              hadn't noticed.
              There is no reason to use 'BinaryWriter' in this particular case.

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

              Comment

              • Guru

                #8
                Re: Write Data To File

                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                news:uJpwh%23AX IHA.2268@TK2MSF TNGP02.phx.gbl. ..
                "Guru" <running@interf erence.nitschri eb:
                >>>>How can I write a byte array to FileStream, I tried to do so and I
                >>>>use the following code:
                >>>[...]
                >>>>
                >>>If you're using VS2005 or later (you didn't say if you were) and you
                >>>want to write a byte array to a file, this is an easier solution:
                >>>>
                >>>\\\
                >>> IO.File.WriteAl lBytes("Winter. jpg", msg)
                >>>///
                >>>
                >>... or 'My.Computer.Fi leSystem.WriteA llBytes', which does pretty much
                >>the same as the above.
                >>
                >What a pair of morons you two are. He's using a BinaryWriter, in case you
                >hadn't noticed.
                >
                There is no reason to use 'BinaryWriter' in this particular case.
                That is not your decision to make, it is the OP's.


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Write Data To File

                  "Guru" <running@interf erence.nitschri eb:
                  >>>>If you're using VS2005 or later (you didn't say if you were) and you
                  >>>>want to write a byte array to a file, this is an easier solution:
                  >>>>>
                  >>>>\\\
                  >>>> IO.File.WriteAl lBytes("Winter. jpg", msg)
                  >>>>///
                  >>>>
                  >>>... or 'My.Computer.Fi leSystem.WriteA llBytes', which does pretty much
                  >>>the same as the above.
                  >>>
                  >>What a pair of morons you two are. He's using a BinaryWriter, in case
                  >>you hadn't noticed.
                  >>
                  >There is no reason to use 'BinaryWriter' in this particular case.
                  >
                  That is not your decision to make, it is the OP's.
                  It's simply a bad idea to use 'BinaryWriter' if you want to persist an array
                  of bytes forming a file's contents.

                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                  Comment

                  • Cor Ligthert[MVP]

                    #10
                    Re: Write Data To File

                    Herfried,

                    What did you often write, I have showed it you by email lately, although I
                    could not resist a minute ago a reply to the troll, let us think about that.

                    Cor

                    Comment

                    Working...