Matt H. Matt H. - 1 year ago 105 Question

.NET make a copy of an embedded file resource to the local drive

i'm new to the realm to working with Files in .NET

I'm creating a WPF application in VB.NET with the 3.5 Framework. (If you provide an example in C#, that's perfectly fine.)

In my project I have a Template for an MS Access database. My desired behavior is that when the users clicks File-->New, they can create a new copy of this template, give it a filename, and save it to their local directory.

The database already has the tables and some starting data needed to interface with my application (a user-friendly data editor)

I'm thinking the approach is to include this "template.accdb" file as a resource in the project, and write it to a file somehow at runtime?

Any guidance will be very, very appreciated.


Answer Source

be sure to pass in the fully qualified namespace name to ResourceName

Public Function ExtractResourceToDisk(ByVal ResourceName As String, ByVal FileToExtractTo As String) As Boolean

    Dim s As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
    Dim ResourceFile As New System.IO.FileStream(FileToExtractTo, IO.FileMode.Create)

    Dim b(s.Length) As Byte

    s.Read(b, 0, s.Length)
    ResourceFile.Write(b, 0, b.Length - 1)

    ResourceFile = Nothing
End Function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download