none
exe.conconfig正由另一进程使用,因此该程无法访问此文件 RRS feed

  • 问题

  •             XmlDocument doc = new XmlDocument();
                StreamReader sr = new StreamReader(Application.ExecutablePath + ".config");
                doc.Load(sr);
                XmlNode node = null;
                string name="SAWP";
                node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
                //node.RemoveAll();
                //doc.Save(Application.ExecutablePath + ".config");
                XmlNode node2 = (doc.SelectSingleNode("configuration")).SelectSingleNode("appSettings");
                node2.RemoveChild(node);
                doc.Save(Application.ExecutablePath+".config");
    想保存修改后的配置文件,但一直提示无法访问
    2014年5月30日 16:05

答案

全部回复

  •         private void UpdateConfig(string name, string Xvalue)
            {
                XmlDocument doc = new XmlDocument();
                StreamReader sr = new StreamReader(Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + "\\App.config");
                doc.Load(sr);
                XmlNode node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
                XmlElement ele = (XmlElement)node;
                ele.SetAttribute("value", Xvalue);
                doc.Save(Application.ExecutablePath + ".config");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                UpdateConfig("SAWD","30");
    
                XmlDocument doc = new XmlDocument();
                StreamReader sr = new StreamReader(Application.ExecutablePath + ".config");
                doc.Load(sr);
                XmlNode node = null;
                string name="SAWP";
                node = doc.SelectSingleNode(@"//add[@key='" + name + "']");
                //node.RemoveAll();
                //doc.Save(Application.ExecutablePath + ".config");
                XmlNode node2 = (doc.SelectSingleNode("configuration")).SelectSingleNode("appSettings");
                //node2.RemoveChild(node);
                //doc.Save(Application.ExecutablePath+".config");
                
                XmlNodeReader xmlread = new XmlNodeReader(node2);
                while (xmlread.Read())
                {
                    if (xmlread.Name == "add")
                    {
                        textBox1.Text = textBox1.Text+xmlread.Name + " " + xmlread.GetAttribute("key") + "   " + xmlread.GetAttribute("value")+"\r\n";
                    }
                }
                //doc.Save(Application.ExecutablePath + ".config");
            }

    为什么这样又不会出现问题?

    2014年5月30日 16:21
  • 已解决,在doc.Load(sr) 后面加上sr.Close();
    • 已标记为答案 ywwangkevin 2014年5月30日 17:59
    2014年5月30日 17:59
  • 感谢分享经验。

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月2日 3:15
    版主