且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#Windows窗体应用程序中,如何获得资源文件夹的路径?

更新时间:2023-02-10 23:15:50

你能否使用 Application.StartupPath 获取应用程序的当前执行路径



使用 System.Reflection System.IO 你也可以找到这样的路径...



Can you please use Application.StartupPath to get current execution path of your application

By using System.Reflection and System.IO you can also find path like this...

string assmblypath = Assembly.GetEntryAssembly().Location;
string appPath = Path.GetDirectoryName(assmblypath);


你已经有了回答,但我会给你一个替代方案,一个更普遍的方法。请参阅我过去的回答:如何查找我的程序目录 [ ^ ]。



请阅读我对你想要这种方式的原因的解释。



现在,如果您通过.resx资源节点使用嵌入式资源,则可以完全摆脱文件位置问题并自行处理文件。您***使用添加现有文件来添加资源,在这种情况下,将文件添加到项目中(作为文件及其对项目文件的引用)和资源文件中的引用;并使用即用型静态属性创建自动生成的C#。如果系统不知道您的资源类型,则将使用字节类型数组,您可以使用该数组从内存流中读取资源。它会很好用,但当然,如果你的音频文件不是太大。



-SA
You already got an answer, but I'll give you an alternative, a more universal method. Please see my past answer: How to find my programs directory[^].

Please read my explanation on why you want to prefer this way.

Now, you could totally get rid of the file location problems and dealing with files themselves if you used embedded resources via .resx resource node. You should better used "Add Existing File" for adding the resource, In this case, the file is added to the project (as a file and its reference to the project file) and the reference in the resource file; and auto-generated C# is created, with the ready-to-use static property. If the type of your resource is not known to the system, the array of byte type will be used, which you can use for reading the resource form a memory stream. It would work great, but if, of course, your audio files are not too big.

—SA


只需要使用:

Simply you need to use:
Properties.Resources.<your filename>

>