如何将文件添加到exe [英] How do I add a file to a exe

查看:354
本文介绍了如何将文件添加到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.

这篇关于如何将文件添加到exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆