Question

I've added a test.zip to a C# project by creating a Resource1.resx and dragged to the resx tab. It is now visible in the Solution Explorer as a child of Resources.

When the program runs, I'd like to move it from the .exe to a location on the computer like My Documents.

I've a feeling I need to convert the resource to a memory stream before I can write it to file but I'm not sure how to access the file as resource or how to convert it.

I think the following extracts the resource object (then again, it doesn't error no matter what the first param is) but I'm not sure how to proceed:

var resource = new ResourceManager("test", Assembly.GetExecutingAssembly());
Was it helpful?

Solution

Since you activate the resources you have already a ResourceManager.Just use GetObject method,get the bytes of your file and write the them to a new file with File.WriteAllBytes:

var bytes =  Properties.Resources.ResourceManager.GetObject("resourceName") as byte[];
File.WriteAllBytes("newFile.zip", bytes);

OTHER TIPS

You should use Assembly.GetManifestResourceStream.

using (Stream x = Assembly.GetExecutingAssembly().GetManifestResourceStream("test"))
{
    ...
}

Reference to MSDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top