VB icon

Save Embedded Resource to File

Email
Submitted on: 1/2/2015 1:48:00 PM
By: Victor Boba (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VB.NET
Views: 912
 
     To save an Embedded Resource and save to file. This will take any embedded resource and save it to any filename you want.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Save Embedded Resource to File
// Description:To save an Embedded Resource and save to file. This will take any embedded resource and save it to any filename you want.
// By: Victor Boba (from psc cd)
//**************************************

' Notes: 
' The file that you add to the project has to have the Build Action
' property changed to "Embedded Resource" for this to work. This will add the file
' to the project as a resource and not a compiled item.
'
Dim ResStream As System.IO.Stream
' This is the name of your applications namespace followed by the name of the file that's embedded.
' So if your namespace is "MyProject" and the name of the file is "BlankDatabase.mdb" then
' the value for the sResPath would be "MyProject.BlankDatabase.mdb".
Dim sResPath As String = "AnimalControl.db.mdb"
Dim NewFilePathName As String = sPath
Dim numBytesRead As Integer = 0
' Get the Embedded Resource
ResStream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(sResPath)
Dim numBytesToRead As Integer = CInt(ResStream.Length)
Dim bytes(ResStream.Length) As Byte
While numBytesToRead + 1 > 0
Dim n As Integer = ResStream.Read(bytes, numBytesRead, numBytesToRead)
' The end of the file has been reached.
If n = 0 Then
Exit While
End If
numBytesRead += n
numBytesToRead -= n
End While
' Save the resource to file
Dim fs As New FileStream(NewFilePathName, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.