在 .Net 中使用相对文件路径

发布于 2024-08-11 15:16:38 字数 491 浏览 2 评论 0原文

我有一个 C# 项目,其中包含一些需要使用 Uri 类引用的资源。对于这个特定问题,我在项目根目录的“Shaders”文件夹中放置了一些着色器,但我之前在处理图像等其他文件时也遇到过这个问题。到目前为止,我已经使用了简单的解决方案,给出了一个固定的解决方案绝对路径,并确保该文件存在于该位置。不用说 - 这不是一个好的解决方案,从长远来看它不会起作用......

那么,我如何使用相对路径来引用我的资源?我想我的问题是双重的:

  • 首先;我不想引用从调试文件夹到项目文件夹的相对路径。我希望将文件复制到构建文件夹。着色器包含在项目中,但显然这还不够。如何告诉项目在构建时复制文件?或者;解决这个问题的常见方法是什么?

  • 第二;当我将“Shaders”文件夹复制到我的构建文件夹时 - 我使用什么 Uri 语法来引用例如放置在该文件夹中的“myShader.ps”?我可以简单地说file:///Shaders/myShader.ps吗?

I have a C# project where I include some resources which I need to refer using the Uri-class. For this specific problem I have some shaders placed in a "Shaders" folder in the root of the project, but I've had this problem before with other files like images, etc. This far I've used the simple solution giving a fixed absolute path, and making sure the file is present at that location. Needless to say - this is not a good solution, and it won't work in the long run...

So, how can I use relative paths to refer my resources? I guess my question is twofold:

  • First; I don't want to refer the relative path from my Debug folder to the project folder. I want the file to be copied to the build folder. The shaders are included in the project, but obviously this isn't enough. How do I tell the project to copy the files when building? Or; what's the common way of solving this?

  • Second; when I have the "Shaders" folder copied to my build folder - what Uri syntax do I use to refer e.g. "myShader.ps" placed inside this folder? Can I simply say file:///Shaders/myShader.ps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦等七年七年为一梦 2024-08-18 15:16:38

要从应用程序目录获取 Uri,请执行以下操作:

Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
Uri shader = new Uri(baseUri, "shaders/test.ps");

To get a Uri from your applications directory do:

Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
Uri shader = new Uri(baseUri, "shaders/test.ps");
寂寞笑我太脆弱 2024-08-18 15:16:38

回答第一条:

如何告诉项目在构建时复制文件?

将文件添加到项目中,右键单击,选择属性并将复制到输出目录更改为始终复制

Answer to first:

How do I tell the project to copy the files when building?

Add the file to the project, right click, select Properties and change Copy to Output Directory to Always Copy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文