You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写带多子节点的C# appSettings?exe.config特定节点写入问题

Modifying exe.config to Add Siemens DLL Paths (With Node Creation If Missing)

Got it, let's work through how to modify your exe.config to add those Siemens DLL paths—whether the target node structure exists already or you need to build it from scratch. Here's a practical approach using C# (the standard for .NET config file manipulation):

Step 1: Load the Config File

First, we'll use XmlDocument to directly manipulate the config file's XML structure, which gives us full control over creating nodes and updating attributes:

using System.Xml;
using System;

// Load the active exe.config file
XmlDocument configDoc = new XmlDocument();
string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
configDoc.Load(configPath);

Step 2: Check for Existing Node Structure

Define the XPath for your target node (replace this with your actual node hierarchy). For example, if you need a structure like <configuration><siemensPaths><path href="..."/></siemensPaths></configuration>, use this:

// Replace with your actual XPath for the parent node of your path elements
string targetParentXPath = "/configuration/siemensPaths";
XmlNode parentNode = configDoc.SelectSingleNode(targetParentXPath);

// If the parent node doesn't exist, build the full structure
if (parentNode == null)
{
    // Get the root <configuration> node
    XmlNode configRoot = configDoc.SelectSingleNode("/configuration");
    
    // Create the <siemensPaths> parent node
    XmlElement siemensPathsNode = configDoc.CreateElement("siemensPaths");
    configRoot.AppendChild(siemensPathsNode);
    
    parentNode = siemensPathsNode;
}

Step 3: Add/Update Path Nodes with href Attributes

Now we'll add new path nodes (or update existing ones if you need to change their href values):

// List of Siemens DLL paths you need to add
string[] dllPaths = new[] 
{ 
    @"C:\Siemens\Automation\FirstDLLPath", 
    @"C:\Siemens\Automation\SecondDLLPath" 
};

foreach (string path in dllPaths)
{
    // Check if a path node with this href already exists (optional, to avoid duplicates)
    XmlNode existingPathNode = parentNode.SelectSingleNode($"path[@href='{path}']");
    
    if (existingPathNode == null)
    {
        // Create a new <path> element and set its href attribute
        XmlElement pathElement = configDoc.CreateElement("path");
        pathElement.SetAttribute("href", path);
        parentNode.AppendChild(pathElement);
    }
    else
    {
        // If you need to update an existing href value, uncomment this line
        // existingPathNode.Attributes["href"].Value = path;
    }
}

Step 4: Save the Modified Config

Finally, save the changes back to the exe.config file:

configDoc.Save(configPath);

Key Notes:

  • Adjust XPath & Node Names: Replace siemensPaths and path with your actual node names from the config schema you need.
  • Permissions: Ensure your application has write access to the directory containing exe.config (especially if it's in Program Files—you may need admin rights).
  • .NET Core/.NET 5+: If you're using a newer .NET version, install the System.Configuration.ConfigurationManager NuGet package if you need additional config helpers, but the XmlDocument approach works across all versions.

内容的提问来源于stack exchange,提问作者Stefan Grönberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:27:54