Answered embedding files to copy to folder when button clicked

chrisakak9

New member
Joined
Sep 30, 2020
Messages
1
Programming Experience
Beginner
Hi everyone,

basic question, I haven't done any programming for about 9 years since leaving uni and tring to find the best easiest way to embed files within a project that I can copy to a folder when a button is clicked.

I currently have everything working as needed using eternal files in one folder being copied to another but ideally I would like everything to be contained within the exe.

please could come one pint me in the right direction.

Thanks
 
The .NET Framework supports using resources. It can take any file and embed it into the compiled assembly. Visual Studio makes it super easy to do the embedding by just changing the "Build Action" to "Embedded Resource".

Of course, once it is embedded, you'll need to change the way you access the file. You now have to get a stream to the resource using Assembly.GetExecutingAssembly().GetManifestResourceStream() and read from the stream instead using File.OpenRead() to get a stream.

See my tiny project to see an example of 4 different text files embedded as resources for my passphrase generator.
 
There's also Resources in project properties where you can paste in files. These can be accessed with strongly typed generated class Properties.Resources using the name of the file added. Depending on FileType text/binary the resource will return as string or byte array, and can be written to file system for example with the File class:
sample:
System.IO.File.WriteAllText(@"d:\file.txt", Properties.Resources.textfile);
System.IO.File.WriteAllBytes(@"d:\file.bin", Properties.Resources.binaryfile);
 
Back
Top Bottom