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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

738 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