views:

71

answers:

4

Hi, 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.

Thanks!

+2  A: 

Well the "easiest" thing would be just to include it as an output file in your program installation so that it gets installed somewhere when your application does (perhaps in your application's directory or a subdirectory off of it). Then you can simply use a File.Copy when you need to create a new one of the template. I'm not advocating that this is the best solution, simply the least amount of work. You can read up on the File.Copy method here.

NebuSoft
+1  A: 

One I've answered before:

http://stackoverflow.com/questions/1802808/new-access-database-how-can-it-be-done/1803097#1803097

Interestingly that's a version hacked about from the original VB, but I don't have the VB to hand (long story).

I'd go a bit further, and suggest that you want to create your schema in code too - i.e. just dump out an empty database and then create the schema therein using DDL.

This C# and for SQL Server version - again I don't have the access/vb.net version to hand

http://stackoverflow.com/questions/1715691/how-to-create-embedded-sql-2008-database-file-if-it-doesnt-exist/1716021#1716021

Murph
+1  A: 

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.Flush()
    ResourceFile.Close()

    ResourceFile = Nothing
End Function
gbogumil
Beautiful solution!!!! It worked on the first try. You people are so smart :p
Matt H.
+1  A: 
public static void WriteResourceToFile(Assembly assembly, Type scope, string resname, string path)
{
    Stream res = assembly.GetManifestResourceStream(scope, resname);
    using (FileStream file = new FileStream(path, FileMode.Create))
    {
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = res.Read(buffer, 0, buffer.Length)) > 0)
        {
            file.Write(buffer, 0, bytesRead);
        }
    }   
}

Should do the trick. Where scope would be a namespace scope for the resource name which allows you to shorten the resource name, which is fully qualified.

Todd