简体   繁体   中英

Confused about embedded resources

I have a class library that contains some generic proceesing functionality - call it "Engine".

I include the class library in a number of web applications.

The engine library needs an XML file as input, but the content is unique to each project.

At the moment I manually copy the XML file into each project. The engine always looks for a file in the application route.

However, I've gotten a little confused with regards to embedded resources. In order to validate the XML, I've created an XSD in my engine project and set the Build Action to EmbeddedResource.

I can't see the difference between setting the BuildAction to Content and EmbeddedResource in this case, which has led me to doubt the way that things are currently set up.

I've not a lot of experience at this level, so need some guidance. Any advice would be appreciated.

EmbeddedResource means that the xsd is embedded inside the assembly during build, while Content means it is just copied along to the output folder. You want the embedded resource thing it sounds like.

You can access Embedded resources through code like this:

string resourceName = "SomeNameSpace.SomeFile.xsd";

Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
    if ( stream == null )
        throw new ArgumentException("resource not found", "resourceName");
    using (StreamReader reader = new StreamReader(stream))
    {
        string result = reader.ReadToEnd();
        return result;
    }
}

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