convert a file(word doc, image etc) to byte array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prabunewindia
    New Member
    • Mar 2007
    • 199

    convert a file(word doc, image etc) to byte array

    hi friends,
    i want to store the file in byte array.
    plz help me. i am thinking we have t odo it thru file stream.
    i tried. but not getting how to use filestream
    plz help
    Prabu
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    You can try this function

    Public Shared Sub Main()
    Dim inStream As FileStream = File.OpenRead(" C:\test.txt")
    Dim outStream As FileStream = File.OpenWrite( "C:\test.tx t" + ".bak")
    Dim buf(4096) As Byte
    Dim bytesRead As Integer
    While (bytesRead = inStream.Read(b uf, 0, 4096)) > 0
    outStream.Write (buf, 0, bytesRead)
    End While
    outStream.Flush
    outStream.Close
    inStream.Close
    End Sub

    Comment

    • prabunewindia
      New Member
      • Mar 2007
      • 199

      #3
      hai swetha,
      i did that by this coding(in C#)
      FileStream fs = new FileStream(file Path, FileMode.Open, FileAccess.Read );
      int length = (int)fs.Length;
      byte[] content = new byte[length];
      fs.Read(content , 0, length);

      exactly what u coded in vb.net.
      thanks for reply

      Prabu
      Originally posted by shweta123
      Hi,

      You can try this function

      Public Shared Sub Main()
      Dim inStream As FileStream = File.OpenRead(" C:\test.txt")
      Dim outStream As FileStream = File.OpenWrite( "C:\test.tx t" + ".bak")
      Dim buf(4096) As Byte
      Dim bytesRead As Integer
      While (bytesRead = inStream.Read(b uf, 0, 4096)) > 0
      outStream.Write (buf, 0, bytesRead)
      End While
      outStream.Flush
      outStream.Close
      inStream.Close
      End Sub

      Comment

      Working...