Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I need help reading a word document and then using FileStream to pass it to a function as a Byte() in VB.Net

Posted on 2016-09-20
2
Medium Priority
?
74 Views
Last Modified: 2016-09-27
Hello Experts,
I have a word document in the following file path; "C:\MyFiles\TestFile.docx"

I want to use FileStream in my VB.Net application to read the file and then pass it to a function that
takes Byte() as an argument (function below).

Private Function SendRequest(DataBytes As Byte())
      'Do Something....

End function



Thank you very much in Advance,
mrotor
0
Comment
Question by:mainrotor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 41807776
File.ReadAllBytes(fileName); will give a byte array of the file
0
 
LVL 21

Accepted Solution

by:
Tapan Pattanaik earned 2000 total points
ID: 41808105
Hi mainrotor,
Please find the below Code.
''' <summary>
''' Function to get byte array from a file
''' </summary>
''' <returns>Byte Array</returns>
Public Function FileToByteArray(_FileName As String) As Byte()
      Dim _Buffer As Byte() = Nothing

      Try
            ' Open file for reading
            Dim _FileStream As New System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)

            ' attach filestream to binary reader
            Dim _BinaryReader As New System.IO.BinaryReader(_FileStream)

            ' get total byte length of the file
            Dim _TotalBytes As Long = New System.IO.FileInfo(_FileName).Length

            ' read entire file into buffer
            _Buffer = _BinaryReader.ReadBytes(DirectCast(_TotalBytes, Int32))

            ' close file reader
            _FileStream.Close()
            _FileStream.Dispose()
            _BinaryReader.Close()
      Catch _Exception As Exception
            ' Error
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
      End Try

      Return _Buffer
End Function

Note: Converting to  Byte array will help you to store binary file in database, send to other systems .. etc
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question