AutoPuTTY.formMain.XmlConfigSet C# (CSharp) Метод

XmlConfigSet() публичный Метод

public XmlConfigSet ( string id, string val ) : void
id string
val string
Результат void
        public void XmlConfigSet(string id, string val)
        {
            string file = Settings.Default.cfgpath;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(file);

            XmlElement newpath = xmldoc.CreateElement("Config");
            XmlAttribute name = xmldoc.CreateAttribute("ID");
            name.Value = id;
            newpath.SetAttributeNode(name);
            newpath.InnerText = val;

            XmlNodeList xmlnode = xmldoc.SelectNodes("//*[@ID=" + ParseXpathString(id) + "]");
            if (xmlnode != null)
            {
                if (xmldoc.DocumentElement != null)
                {
                    if (xmlnode.Count > 0) {
                        xmldoc.DocumentElement.ReplaceChild(newpath, xmlnode[0]);
                    }
                    else
                    {
                        xmldoc.DocumentElement.InsertBefore(newpath, xmldoc.DocumentElement.FirstChild);
                    }
                }
            }

            try
            {
                xmldoc.Save(file);
            }
            catch (UnauthorizedAccessException)
            {
                Error("Could not write to configuration file :'(\rModifications will not be saved\rPlease check your user permissions.");
            }
        }

Usage Example

Пример #1
0
 private void tbPuTTY_TextChanged(object sender, EventArgs e)
 {
     Settings.Default.puttypath = tbPuTTYPath.Text;
     if (!firstread)
     {
         mainform.XmlConfigSet("putty", Settings.Default.puttypath);
     }
 }