tags:

views:

17

answers:

1

I use this function to convert file to bytes array:

Public Function ConvertToBytes(ByVal path As String) As Byte()
        Dim _tempByte() As Byte = Nothing
        If String.IsNullOrEmpty(path) = True Then
            Throw New ArgumentNullException("File not exist", path )
            Return Nothing
        End If
        Try
            Dim _fileInfo As New IO.FileInfo(path )
            Dim _NumBytes As Long = _fileInfo.Length
            Dim _FStream As New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read)
            Dim _BinaryReader As New IO.BinaryReader(_FStream)
            _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
            _fileInfo = Nothing
            _NumBytes = 0
            _FStream.Close()
            _FStream.Dispose()
            _BinaryReader.Close()
             Return _tempByte
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

It's all working all right when file is not shared but when files are shared I go to exception from this code line:

Dim _FStream As New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read)

What is problem with my function?

Thanks!

A: 

You haven't said that FileStream should allowed to be shared. There's a separeate overload for that:

FileStream(String, FileMode, FileAccess, FileShare)

But wouldn't it be easier to use the built-in method for this:

IO.File.ReadAllBytes(String)
danbystrom