且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从文件中获取流

更新时间:2023-02-23 21:31:41

To Stream:

  Dim  fs 作为 IO.FileStream 
fs = FileStream( 文件路径,FileMode.Open)





To字节数组:

*使用FileSystem

  Dim  readBytes(LengthYouWantToRead  -   1 )作为 字节 
fs .Read(readBytes, 0 ,LengthYouWantToRead)





*使用IO。文件

  Dim  readBytes() As  字节 
readBytes = IO.File.ReadAllBytes( 文件路径





(FileStream示例)

如果你想阅读来自文件的4096字节,

这样编写你的代码:



  Dim  readBytes( 4096   -   1 作为 字节 
fs.Read(readBytes, 0 4096


检查这些:

1. saving-and-retrieving-file-使用-FILESTREAM-SQL- server-2008.aspx [ ^ ]

2. add-and-reading-files-from-sql-server-2008-filestream [ ^ ]


您可以直接使用 ReadAllBytes()文件Stream的 System.IO.File 的共享功能。请参阅以下代码:

  Dim  _mFileBytes  As   Byte ()= System.IO.File.ReadAllBytes( 您的文件路径



之后您可以直接保存此字节数组(在数据库中的上述示例 _mFileBytes 中,其列具有数据类型 Varbinary()



欲了解更多详情:如何在SQL Server数据库表(VB.NET)中插入字节数组。 [ ^ ]



我希望这会对你有所帮助。 :)


i want to ask..
how do i get stream from file in vb.net?
i want to save that stream in database..and in my database, i set data type varbinary (max) to save stream

hope you can help me :)
thanks very much

To Stream:
Dim fs As IO.FileStream
fs = New FileStream("File Path", FileMode.Open)



To Byte Array:
* Using FileSystem

Dim readBytes(LengthYouWantToRead - 1) As Byte
fs.Read(readBytes, 0, LengthYouWantToRead)



* Using IO.File

Dim readBytes() As Byte
readBytes = IO.File.ReadAllBytes("File Path")



(FileStream Example)
If you want to read 4096 Bytes from file,
write your code like this:

Dim readBytes(4096 - 1) As Byte
fs.Read(readBytes, 0, 4096)


Check these out:
1. saving-and-retrieving-file-using-filestream-sql-server-2008.aspx[^]
2. adding-and-reading-files-from-sql-server-2008-filestream[^]


You can Directly use ReadAllBytes() Shared Function of System.IO.File insted of file Stream. See the below code :
Dim _mFileBytes As Byte() = System.IO.File.ReadAllBytes("Your File Path")


After that you can directly save this Byte Array (In above example _mFileBytes) in the Database with the column having Data Type Varbinary().

For more Details : How to Insert Byte Array In SQL Server Database Table(VB.NET).[^]

I hope this will help you. :)