Embed File in a Executable
c#, embedded-resource, file, forms
Solution
You want to do embed the file as a resource.
Right-click the project file, select Properties.
In the window that opens, go to the Resources tab, and if it has just a blue link in the middle of the tab-page, click it, to create a new resource.
In your code you can type in Resources.TheNameYouGaveTheFileHere and you can access its contents. Note that the first time you use the Resources class in a class, you need to add a using directive (hit Ctrl+. after typing Resources to get the menu to get VS to do it for you).
Do you need help with the saving of the file also?
Edit:
You could do something like this:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
for (int i = 0; i < resource.Length; i++)
fileStream.WriteByte((byte)resource[i]);
fileStream.Close();
Does it help you?
EDIT:
As I can see you are getting a stream, here is an update to make it work:
var resource = Properties.Resources.yourResource;
FileStream fileStream = new FileStream("filename.exe", FileMode.CreateNew);
resource.CopyTo(fileStream);
fileStream.Close();
Does it work now?
Problem
I'm trying to build a simple program and I want to find a way to embed a file (or multiple files) in the executable. The program is very simple. I will be building a form using C# in visual studio. On the form, there will be couple questions and a submit button. Once the user has answer all the questions and hit the submit button, if all answers are correct, I want to give the user the file as a prize. (The file can be image, video, or a zip file that contains multiple other files) The way I want to give the user the file is very flexible. It can just be creating this file in the same directory as the executable, or given the download option for the user to save it somewhere else. Below is the pseudo code ``` private void submit_Click(object sender, EventArgs e) { //functions to check all answers if(all answers are correct) { label.Text = "Congrats! You answered all questions correctly"; //create the file that was embeded into the same directory as the executable //let's call the file 'prize.img' Process.Start("prize.img"); } else label.Text = "Some answers were not correct"; } ``` The logic is pretty simple and straight forward. The problem is, how can I embed "`prize.img`" into the executable? I will be giving this program (.exe) to a friend so he will not have any source and I can't guarantee the path.