views:

284

answers:

3

It is actually useful for me to store some files in EXE to copy to selected location. I'm generating HTML and JS files and need to copy some CSS, JS and GIFs.

Snippet

System.IO.File.WriteAllBytes(@"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);

doesn't work for me!

On "WriteAllBytes" it says: "cannot convert from 'System.Drawing.Bitmap' to 'byte[]'" for image and "cannot convert from 'string' to 'byte[]'" for text file.

Help!

UPDATE: Solved below.

+5  A: 

Add the files you want to your solution and then set their Build Action property to Embedded Resource. This will embed the file into your exe. (msdn)

Then you just need to write the code to write the file out to disk when the exe is executed.

Something like:

File.Copy("resource.bmp", @"C:\MyFile.bin");

Replace resource.bmp with your file name.

Addendum:

If you keep the file in a sub-folder in your solution you need to make the sub-folder part of the path to resource.bmp. Eg:

File.Copy(@"NewFolder1\resource.bmp", @"C:\MyFile.bin");

Also, you may need to set the Copy To Output Directory property to Copy Always or Copy If Newer.

Phil
You right, it must be "Embedded Resource", but just File.Copy() doesn't work here.I found the solution.
It works for me... Did you change the "resource.bmp" to the right file name?
Phil
Shure. But it doesn't.Seems that File.Copy() works only with textfiles.I left my solution here.
What happens if you don't copy a text file? I tried the above with a bmp.
Phil
You don't know? So how do you know it doesn't work?
Phil
Ehh, man... I tried exactly as you described and your solution DOESN'T work for me. Then I found out how to do it an post my solution below. It is 100% working with any kind of file — binary and text. Now my tiny, yeah, very tiny and quite unnecessary problem SOLVED. There is too much to do ahead. BTW I took a look. FileNotFound exception pops on file: File.Copy("somefile.gif", someDestPath); Even if as Project_Namespace.Resources.somefile.gif Yeah, file IS embedded and it is framework 2.0
A FileNotFound exception? Did you put the files into a sub-folder in your solution?
Phil
Of course in sub-folder and it is embedded.
Ok, then in this case you need to reflect that in your path to the source file. I've updated the answer in case somebody else comes across this problem.
Phil
A: 

You can embed binary files in a .resx file. Put them in the Files section (it looks like you used the Images section instead). It should be accessible as an array of bytes if your .resx file generates a .Designer.cs file.

File.WriteAllBytes(@"C:\foobar.exe", Properties.Resources.foobar);
Francis Gagné
+1  A: 

Add files to project resources and set their "Build Action" as "Embedded Resource".

Now extract any file (text or binary) using this snippet:

WriteResourceToFile("Project_Namespace.Resources.filename_as_in_resources.extension", "extractedfile.txt");


public static void WriteResourceToFile(string resourceName, string fileName)
{
    int bufferSize = 4096; // set 4KB buffer
    byte[] buffer = new byte[bufferSize];
    using (Stream input = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
    using (Stream output = new FileStream(fileName, FileMode.Create))
    {
        int byteCount = input.Read(buffer, 0, bufferSize);
        while (byteCount > 0)
        {
            output.Write(buffer, 0, byteCount);
            byteCount = input.Read(buffer, 0, bufferSize);
        }
    }
}

Don't know how deep is it correct according to this article: http://www.yoda.arachsys.com/csharp/readbinary.html but it works.