How do I add a file to a exe

2019-03-06 09:24发布

问题:

I have a program that plays a sound but if I run it on a different computer it says that the file isn't found how do I attach the file onto the exe so when someone plays the exe it will still be able to play?

回答1:

When Packaging your project files create a folder inside your project folder to hold your content data (text - pictures - videos - sound .. etc) and then supply your code with the relative path of your target file rather than the absolute path .

for example : instead of

    string path = "c:\Projects\ProjectFolder\FileName";

do it like that :

     string currentDir = AppDomain.CurrentDomain.BaseDirectory;
     string fullPath = currentDir + "ContentFolder/FileName.mp3(or whatever)";    

Edit :

    AppDomain.CurrentDomain.BaseDirectory

Will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client app, it will be the directory containing the main executable.



回答2:

If you are wanting to embed your wave file into your program, go to your Project Properties --> Resources --> Select Audio as the type then select Add Resource and select your Audio File. This will add the Audio File to your Resources Directory. Once you have done this you can right click on the File in the Resources folder and select Properties and change the Build Action to Embedded Resource.

To access the file you would do something like this:

System.Media.SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.tada);
sp.Play();



回答3:

You can go the properties of the file, and under Copy to output Directory select, copy if newer

You can package the EXE with your c# program when you are ready to release it with an installer



回答4:

If you cannot change the source code,I suggest

http://m.instructables.com/id/How-to-create-portable-app/?ALLSTEPS

It is about creating new exe archive containing old exe+new files you want to merge. When you call that new exe, it runs your old exe, but also contain your music files You can follow step4 and 5

Put your exe and music files into the same folder and compress them through that method



标签: c# file embed exe