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
57 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
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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now