简体   繁体   中英

How do I use a file included in my visual studio project?

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM