How do I add a file to a exe

2019-03-06 09:30发布

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?

标签: c# file embed exe
4条回答
甜甜的少女心
2楼-- · 2019-03-06 09:43

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.

查看更多
够拽才男人
3楼-- · 2019-03-06 09:46

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

查看更多
Fickle 薄情
4楼-- · 2019-03-06 09:48

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

enter image description here

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

查看更多
再贱就再见
5楼-- · 2019-03-06 09:49

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();

enter image description here

enter image description here

enter image description here

查看更多
登录 后发表回答