我需要找到一种方法来为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&gt;中实际获取值。编译&gt;装配部分!
有什么建议吗?
答案 0 :(得分:3)
有一个名为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");
Cherio