Embedded Resources
Add a file to the project with Build Action = "Embedded Resource". The namespace is the project namespace + the folder it's in.
using System.IO;
using System.Reflection;
using System.Xml;
namespace MyProject.Resources
{
class EmbeddedResourceLoader
{
private readonly Assembly _assembly;
public EmbeddedResourceLoader(Assembly assembly = null)
{
//assume resource is in this assembly, but caller can specify if required
if (assembly == null)
assembly = GetType().Assembly;
_assembly = assembly;
}
public Stream LoadStream(string name)
{
return _assembly.GetManifestResourceStream(name);
//// eg write it to a file
//using (var sw = new StreamWriter(fileName))
//{
// sw.Write(stream);
//}
//// or create image
//var image = Image.FromStream(stream);
}
public XmlReader LoadXml(string name)
{
var stream = _assembly.GetManifestResourceStream(name);
if (stream == null) return null;
return XmlReader.Create(stream);
////create a XDocument
//var doc = XDocument.Load(xmlReader);
////xsd into schema
//var schemas = new System.Xml.Schema.XmlSchemaSet();
//schemas.Add("", xmlReader);
////and validate
//var isValid = true;
//doc.Validate(schemas, (o, e) => { isValid = false; });
}
public string LoadString(string name)
{
var stream = _assembly.GetManifestResourceStream(name);
if (stream == null) return null;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}