tags:

views:

64

answers:

2

i want to add a .mdb file to the resource, and while running i want to copy .mdb file to another location, do connection run it and delete the copied .mdb file while i finished processing with .mdb file.

how to do this using assembly.GetManifestStream? if not with assembly.GetManifestStream what are the other ways with which i can do this?

A: 

Why can't you just use a file level copy? Not sure why you want to do it with the stream. Just find the path to the mdb file and

File.Copy(path1,path2);
chamiltongt
If the OP wants to embed the file within the output file of his project the method Jason posted works. If not, the OP can set the Build Action of the file to Content, set the appropriate Copy to Output Directory setting and then use a plain old `File.Copy` and then `File.Delete`.
AndrewJacksonZA
the problem is i want to use this in writing unit testcases... i assign that in setup, in teardown i want to delete the created file, while deleting it is file is in use error
+2  A: 

Yes, Assembly.GetManifestStream is the way to go combined with a BinaryReader and a BinaryWriter. To wit:

// assembly is Assembly containing the resource
// path is string containing path to write to
Stream source = assembly.GetManifestResourceStream("Namespace.filename.mdb");
BinaryReader br = new BinaryReader(source);
BinaryWriter bw = new BinaryWriter(path, FileMode.Create);
byte[] buffer = new byte[256];
int count = 0;
while((count = br.Read(buffer, 0, 256)) > 0) {
    bw.Write(buffer, 0, count);
}

Obviously all those IDisposables should be wrapped in a using block.

Jason
Thanks for the answer Jason, I didn't know how to do this for embedded resources either. I tried it and it worked.For those that don't know: just add the file that needs to be extracted to your project and set it's "Build Action" property to "Embedded Resource". I copied abc.def into the directory that contained my .cssproj file, clicked on the "Show all files" button in the Solution Explorer in VS2008 and then right-clicked on abc.def and then included it in my project.
AndrewJacksonZA