views:

160

answers:

3

In the Visual Studio application I'm creating, I want to include an .xsd file that is used in the application.

The xsd file is in the same directory as the rest of my .cs files, and I dragged/dropped it into the Solution Explorer window as an item in my project.

But in my C# code... how do I make use of it? It doesn't seem right to hardcode the location of it on my computer... but just using "myfile.xsd" or ".\myfile.xsd" or various combinations of that didn't seem to work...

Thanks in advance!

-Adeena

+4  A: 

Do you need it to be an actual file? If not, I'd make it an embedded resource (select that in the properties of the item in Visual Studio) and use Assembly.GetManifestResourceStream to load it at execution time. That's neat and tidy for deployment purposes, although it does lose you the flexibility of changing it without rebuilding.

Jon Skeet
I think yes - I want it to be an actual file. It's a schema, and it's the default one that the users of my program would use. But I need them to be able to customize it...
adeena
+2  A: 

There are several ways, but the easiest would be to click the file in Solution Explorer, go the the properties windows and change the Build Action property. Making it content is popular but you could also go the route of an Embedded Resource.

keithwarren7
A: 

If you want it to be user configurable you should probably make a custom build step that copies the file into a common location such as CommonApplicationData. Then reference it from your application in that path. You can get access to those special folders with Environment.GetFolderPath().

http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx

The parameter it takes is an enumeration:

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

CommonApplicationData - The directory that serves as a common repository for application-specific data that is used by all users.

Steven Behnke