如何编写带多子节点的C# appSettings?exe.config特定节点写入问题
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
siemensPathsandpathwith 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.ConfigurationManagerNuGet package if you need additional config helpers, but theXmlDocumentapproach works across all versions.
内容的提问来源于stack exchange,提问作者Stefan Grönberg

