views:

194

answers:

2

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?

+3  A: 

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.

Mark Byers
I'm confused.. I did this already, but it didn't copy. However, the "Build Action" was set to "Resource". I tested some more, and "None" or "Embedded Resource" makes the files copy. Thanks! What's the proper "Build Action" to use?
stiank81
The BuildAction is None.
Mark Byers
+5  A: 

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