我需要找出一种方法来为web.config文件中的程序集节添加CRUD (创建、读取、更新和删除)支持。
它可能看起来像这样
<system.web>
<compilation defaultLanguage="c#" debug="true" batch="false" targetFramework="4.0">
<assemblies>
<add assembly="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
</system.web>我试着从这样的东西开始
public bool AssemblyExist(string name)
{
var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var assemblies = config.GetSection("system.web");
// return true on match
return assemblies.ElementInformation.Properties.Keys.Equals(name);
}但它当然会失败。
因此,我想要的是一个示例,展示如何实际获取system.web > compilation > values部分中的值!
有什么建议吗?
发布于 2011-07-19 23:52:31
有一个名为AssemblyInfo的数据类型,它的关键字!
private bool AssemblyExist(string fullName)
{
var config = WebConfigurationManager.OpenWebConfiguration("~");
var compilationSection = (CompilationSection)config.GetSection("system.web/compilation");
return compilationSection.Assemblies.Cast<AssemblyInfo>().Any(assembly => assembly.Assembly == fullName);
}或者如果在ubmraco中使用它
private bool AssemblyExist(string fullName)
{
var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
var compilationSection = (CompilationSection)config.GetSection("system.web/compilation");
return compilationSection.Assemblies.Cast<AssemblyInfo>().Any(assembly => assembly.Assembly == fullName);
}这样叫它
AssemblyExist("System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089")并添加程序集
private static void AddAssembly(string fullName)
{
var config = WebConfigurationManager.OpenWebConfiguration("~");
var compilationSection = (CompilationSection)config.GetSection("system.web/compilation");
var myAssembly = new AssemblyInfo(fullName);
compilationSection.Assemblies.Add(myAssembly);
config.Save();
}把它叫做
AddAssembly("System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");切里欧
https://stackoverflow.com/questions/6749240
复制相似问题