如何读取dll里资源文件 的几种方法

smileangel_xw 2016-04-15 10:04:34
一、当前程序集
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
...全文
1077 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Poopaye 2016-04-15
  • 打赏
  • 举报
回复
smileangel_xw 2016-04-15
  • 打赏
  • 举报
回复
XW.Common.dll

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧