Opc.Ua.Configuration.ConfigUtils.UpdateConfigurationLocation C# (CSharp) Method

UpdateConfigurationLocation() public static method

Updates the configuration location for the specified
public static UpdateConfigurationLocation ( string executablePath, string configurationPath ) : void
executablePath string
configurationPath string
return void
        public static void UpdateConfigurationLocation(string executablePath, string configurationPath)
        {
            string configFilePath = Utils.Format("{0}.config", executablePath);

            // not all apps have an app.config file.
            if (!File.Exists(configFilePath))
            {
                return;
            }

            // load from file.
            XmlDocument document = new XmlDocument();
            document.Load(configFilePath);

            for (XmlNode child = document.DocumentElement.FirstChild; child != null; child = child.NextSibling)
            {
                // ignore non-element.
                XmlElement element = child as XmlElement;

                if (element == null)
                {
                    continue;
                }

                // look for the configuration location.
                XmlElement location = FindFirstElement(element, "ConfigurationLocation", Namespaces.OpcUaConfig);

                if (location == null)
                {
                    continue;
                }
                
                // find the file path.
                XmlElement filePath = FindFirstElement(location, "FilePath", Namespaces.OpcUaConfig);

                if (filePath == null)
                {
                    filePath = location.OwnerDocument.CreateElement("FilePath", Namespaces.OpcUaConfig);
                    location.InsertBefore(filePath, location.FirstChild);
                }
                
                filePath.InnerText = configurationPath;
                break;
            }
            
            // save configuration file.
            Stream ostrm = File.Open(configFilePath, FileMode.Create, FileAccess.Write);
			XmlTextWriter writer = new XmlTextWriter(ostrm, System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;    

            try
            {            
                document.Save(writer);
            }
            finally
            {
                writer.Close();
            }
        }