如何将文件添加到exe

问题描述:

我有一个播放声音的程序,但是如果我在另一台计算机上运行该程序,则说找不到文件,我该如何将该文件附加到exe上,因此当有人播放该exe时,仍然可以

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?

在打包项目文件时,请在项目文件夹中创建一个文件夹来保存内容数据(文本-图片-视频) -Sound等),然后为您的代码提供目标文件的相对路径,而不是绝对路径。

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 .

例如:代替

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

这样做:

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

编辑:

    AppDomain.CurrentDomain.BaseDirectory

将是应用程序的根目录,而不是bin子文件夹-这可能是您通常想要的。在客户端应用程序中,它将是包含主要可执行文件的目录。

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.