Q:

Modifying values in the assemblies section of web.config with c#

I need to figure out a way to add CRUD (Create, Read, Update and Delete) support for the assemblies section in the web.config file. 
It may look like this
<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>

I've tried to start with something like this
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);
}

But of course it fails. 
So, what I'd like is an example showing how to actually fetch the values in the system.web > compilation > assemblies section!
Any advice?

A:

There was a datatype called AssemblyInfo that where the key!
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);
}

Or if using it in 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);
}

Call it like this
AssemblyExist("System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089")

And to add an assembly
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();
}

To call it
AddAssembly("System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

Cherio

