Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to ask..
how do i get stream from file in
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
Updated 24-Oct-22 1:39am

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)
Share this answer
Share this answer
Peter Yohanes 25-May-14 13:54pm    
okay thank Peter Leow..
it's very help me :)
have you ever use system watcher in :)
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. :)
Share this answer
The following code opens a file using System.IO.FileSystem to Append:
Dim file As System.IO.FileStream
file = New FileStream("streamtest.txt", FileMode.Append)

To write text:
Dim bytes() As Byte
bytes = Encoding.ASCII.GetBytes("Example text")
file.Write(bytes, 0, bytes.Length)

To close the file:

I can not figure out what you meant by "i want to save that stream in database".
Share this answer
Peter Yohanes 23-May-14 2:40am    
in my program, i upload file and it save to database..
so user can download that file again and i must save that varbinary every file correctly..
your solution still can't help me,i just try it..
thanks for your time :)
string fileName = "Dummy.xml";
string resourceName = "Namespace0.Folder.Folder2.DataFolder." + fileName;

var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
Share this answer
Dave Kreskowiak 24-Oct-22 8:36am    
If you would have read the previous answers from over the years, you would have seen the question has to do with actual files on disk, not in application resources.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900