VB - Export resource at runtime
Hi all,
Is anyone aware of a method to export a resource (an .accdb file) at runtime ?
I've had success in exporting image files by using :-
My.Computer.FileSystem.WriteAllBytes("filepath", My.Resources.filename, False)
Unfortunately this method dosen't enable the export of the file type I need. For various reasons the file must be exported at runtime, so any ideas would be helpful.
Thanks
Charlie
Re: VB - Export resource at runtime
Try this
Code:
Private FileName As String = String.Concat(Application.StartupPath, "\YourFileName.accdb")
Code:
My.Resources.MainDocument.FileSave(FileName)
Code module
Code:
Module ResourceExtensions
''' <summary>
''' Writes the contents of an embedded resource embedded as Bytes to disk.
''' </summary>
''' <param name="BytesToWrite">Embedded resource</param>
''' <param name="FileName">Save to file</param>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Sub FileSave(ByVal BytesToWrite() As Byte, ByVal FileName As String)
Dim FileStream As New System.IO.FileStream(FileName, System.IO.FileMode.OpenOrCreate)
Dim BinaryWriter As New System.IO.BinaryWriter(FileStream)
BinaryWriter.Write(BytesToWrite)
BinaryWriter.Close()
FileStream.Close()
End Sub
End Module
Re: VB - Export resource at runtime
What exactly does 'My.Resources.filename' return for your ACCBD resource? Is it maybe an UnmanagedResourceStream or something like that? If so then that object, like any other Stream, has a Read method that will allow you to read its contents into a Byte array. Once you have a Byte array, you can use the same code as before to save it to a file.