Results 1 to 3 of 3

Thread: VB - Export resource at runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    37

    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

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width