Click here to Skip to main content
15,886,689 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 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
Posted
Updated 24-Oct-22 1:39am

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


To Byte Array:
* Using FileSystem
VB
Dim readBytes(LengthYouWantToRead - 1) As Byte
fs.Read(readBytes, 0, LengthYouWantToRead)


* Using IO.File
VB
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:

VB
Dim readBytes(4096 - 1) As Byte
fs.Read(readBytes, 0, 4096)
 
Share this answer
 
 
Share this answer
 
Comments
Peter Yohanes 25-May-14 13:54pm    
okay thank Peter Leow..
it's very help me :)
have you ever use system watcher in vb.net? :)
You can Directly use ReadAllBytes() Shared Function of System.IO.File insted of file Stream. See the below code :
VB
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
 
v2
The following code opens a file using System.IO.FileSystem to Append:
VB
Dim file As System.IO.FileStream
file = New FileStream("streamtest.txt", FileMode.Append)

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

To close the file:
VB
file.Close()

I can not figure out what you meant by "i want to save that stream in database".
 
Share this answer
 
Comments
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
 
Comments
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