开发者

Working with relative file paths in .Net

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?


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");


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜