在 .Net 中使用相对文件路径
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从应用程序目录获取 Uri,请执行以下操作:
To get a Uri from your applications directory do:
回答第一条:
将文件添加到项目中,右键单击,选择
属性
并将复制到输出目录
更改为始终复制
。Answer to first:
Add the file to the project, right click, select
Properties
and changeCopy to Output Directory
toAlways Copy
.