共享文件到bytes数组

时间:2010-01-29 10:44:15

标签: c# .net vb.net

我使用此函数将文件转换为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

当文件未共享但文件共享时,这一切都正常工作我从这个代码行转到异常:

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

我的功能有什么问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

您没有说过应该允许共享FileStream。有一个单独的重载:

FileStream(String, FileMode, FileAccess, FileShare)

但是使用内置方法不会更容易:

IO.File.ReadAllBytes(String)