简体   繁体   English

使用 c# 修改 web.config 的程序集部分中的值

[英]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.我需要想办法为 web.config 文件中的程序集部分添加 CRUD(创建、读取、更新和删除)支持。

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!所以,我想要一个示例,展示如何实际获取系统中的值。web > 编译 > 程序集部分!

Any advice?有什么建议吗?

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

Or if using it in ubmraco或者如果在 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切里奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM