Domanda

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.

È stato utile?

Soluzione

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?

Altri suggerimenti

Can't you add the image file as a resource in your solution? And then you can reference that resource in your code?

See if this may be what you're looking for: Embedding Image Resource in Project

The link provided offers step-by-step how to accomplish this: enter image description here

After adding the image as a resource, the instructions show how to access your image resource and use it in a program. Just click the link above and follow the instructions.

I followed the instructions above and wrote my program like this:

using System.Drawing;
using System.Windows.Forms;
using System.Reflection;

    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                var myAssembly = Assembly.GetExecutingAssembly();
                var myStream = myAssembly.GetManifestResourceStream("WindowsFormsApplication6.Desert.jpg");
                var image = new Bitmap(myStream);

                pictureBox1.Image = image;
            }
        }
    }

My output looks like this:

enter image description here

The image on the form came from the image that I embedded as a resource. Very easy to do.

Add the file into project then set its Build Action as Embeded resource.

From your code you can do something like this:

var _assembly = Assembly.GetExecutingAssembly();
var _stream = _assembly.GetManifestResourceStream("namespace.fileneme");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top