How to access binary resource in c # application

I am trying to save an empty Access database (.mdb) as a resource in my application. Then I want to write it to the file system and populate it with table definitions, etc. The problem that I have in Visual Studio 2005 and my C # application does not have access to the resource stored in the same assembly (which I would seem to be available by default). Here is my code:

byte[] abytResource; System.Reflection.Assembly objAssembly = System.Reflection.Assembly.GetExecutingAssembly(); objStream = objAssembly.GetManifestResourceStream("empty.mdb"); abytResource = new Byte[objStream.Length]; objStream.Read(abytResource, 0, (int)objStream.Length); objFileStream = new FileStream(newDatabasePathWithName, FileMode.Create); objFileStream.Write(abytResource, 0, (int)objStream.Length); objFileStream.Close(); 

GetManifestResourceStream also returns NULL according to the documentation, since the resource must be private (because even if it does not exist, a value other than NULL is returned). So my question is this:

How to make my resource available for my own application? I already added it to the project and marked it as "Embedded Resource", by the way.

Thanks!

+7
resources
source share
3 answers

You need the prefix "empty.mdb" with the default namespace of the assembly. Something like:

 objStream = objAssembly.GetManifestResourceStream("My.Namespace.empty.mdb"); 
+16
source share

You can also check the names of your resources by calling

 string[] myResources = objAssembly.GetManifestResourceNames(); foreach(string reso in myResources) { Console.WriteLine(reso); } 

Also, make sure your empty.mdb file is marked as Embedded Resource when compiling

alt text http://img520.imageshack.us/img520/6649/sinttuloo.png

+9
source share

This will extract the embedded resources binary ...

  /// <summary> /// Extracts an embedded file to local file system. /// </summary> /// <param name="resName">Resource name of embedded file. NameSpace.FileName.Extension</param> /// <param name="fileName">Extracted file name</param> public void ExtractEmbeddedFile(string resName, string fileName) { if (File.Exists(fileName)) File.Delete(fileName); Assembly assembly = Assembly.GetExecutingAssembly(); using (var input = assembly.GetManifestResourceStream(resName)) using (var output = File.Open(fileName, FileMode.CreateNew)) { if (input == null) throw new FileNotFoundException(resName + ": Embedded resoure file not found"); var buffer = new byte[32768]; int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } output.Flush(); } } 

This is very useful for C # Unit tests, which require data files in which your DLL files usually run without any content files (they can run in temporary folders created by the unit test framework).

This has a different code snippet for text files (not binary files), although it may have a memory leak because it could not use the using statement.

+1
source share

All Articles