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
40 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now