How do I add a file to a exe

后端 未结 4 1267
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 12:04

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 ex

相关标签:
4条回答
  • 2021-01-25 12:04

    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

    0 讨论(0)
  • 2021-01-25 12:16

    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.

    0 讨论(0)
  • 2021-01-25 12:20

    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

    0 讨论(0)
  • 2021-01-25 12:23

    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

    0 讨论(0)
提交回复
热议问题