C# 程序集资源
一、添加资源:
为更合理的组织资源,可在项目中创建子文件夹,然后在这些子文件夹中添加不同类型的资源,并将资源的属性生成操作改成Resource;(注意:应用程序被命名为AssemblyResources,资源流被命名为AssemblyResources.g.resources)。例如下图在Image文件夹中添加了一个图片资源;
二、检索资源:
DirectoryInfo folder = new DirectoryInfo(@"D:\Projects\WindowsFormsApplication26\ClassLibrary1\bin\Debug");
foreach (var file in folder.GetFiles("*.dll"))
{
Assembly assembly = Assembly.LoadFile(file.FullName);
string resourceName = assembly.GetName().Name + ".g";
//ResourceManager manager = new ResourceManager(resourceName,this.GetType().Assembly);
ResourceManager manager = new ResourceManager(resourceName, assembly);
var resourceSet = manager.GetResourceSet(CultureInfo.CurrentCulture,true,true);
var dictionaryEntries = resourceSet.OfType<DictionaryEntry>().ToList();
dictionaryEntries.ForEach(arg => {
if(arg.Key.ToString().Contains("png"))
{
Bitmap bitmap = new Bitmap((Stream)arg.Value);
pictureBox1.Image = bitmap;//检索到资源并在pictureBox控件中显示
//de.Key是资源名
//de.Value是资源内容
}
});
}
补充:文件资源属性中生成操作的区别: