How do I add a file to a exe

后端 未结 4 1331
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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.

提交回复
热议问题