如何读取dll里资源文件
本人亲自验证的几种方法,这里已xml文件为例
一、当前程序集
System.Xml. XmlDocument doc = new System.Xml.XmlDocument();//xml直接嵌入程序集 读取的方法
System.IO. Stream sm = Assembly .GetExecutingAssembly().GetManifestResourceStream( "XW.Common.Address.xml" );
doc.Load(sm); //直接将流转成xml
byte[] bs = new byte[sm.Length];
sm.Read(bs, 0, ( int)sm.Length);
sm.Close();
UTF8Encoding con = new UTF8Encoding();
string str = con.GetString(bs);//将流转成字符串
//xml嵌入资源文件里Resource1.resx 读取的两种方法
//1
System.Resources. ResourceManager temp = new System.Resources.ResourceManager( "XW.Common.Resource1" , typeof( Resource1).Assembly);
var str1 = temp.GetObject("Address" );
doc.LoadXml(str1.ToString());
//2
doc.LoadXml( Resource1.ResourceManager.GetObject("Address" ) as string);
二、引用程序集 dll里的资源文件读取
//首先在程序集里添加一个资源文件Resource1.resx 将资源文件改成文件 设置访问public 然后在别的程序集就能调研资源了 我这里是xml文件
System.Resources. ResourceManager rm = new System.Resources.ResourceManager (typeof(XW.Common. Resource1));
var ttr = rm.GetObject("Address" );
doc.Load(sm); //直接将流转成xml