الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

app.config

مغلق
بدأه zaidanz في 31 مارس 2005 · 0 رد · 474 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1

dear all,,,

I am trying to save some file pathes in the app.exe.config but the data is deleted all the time....

I used this class :

using System;

using System.Xml;

using System.Configuration;

using System.Reflection;

public class ConfigSettings

{

private ConfigSettings() {}

public static string ReadSetting(string key)

{

return ConfigurationSettings.AppSettings[key];

}

public static void WriteSetting(string key, string value)

{

XmlDocument doc = loadConfigDocument();

XmlNode node = doc.SelectSingleNode("//appSettings");

if (node == null)

throw new InvalidOperationException("appSettings section not found in config file.");

try

{

XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key={0}]", key));

if (elem != null)

{

elem.SetAttribute("value", value);

}

else

{

elem = doc.CreateElement("add");

elem.SetAttribute("key", key);

elem.SetAttribute("value", value);

node.AppendChild(elem);

}

doc.Save(getConfigFilePath());

}

catch

{

throw;

}

}

public static void RemoveSetting(string key)

{

XmlDocument doc = loadConfigDocument();

XmlNode node = doc.SelectSingleNode("//appSettings");

try

{

if (node == null)

throw new InvalidOperationException("appSettings section not found in config file.");

else

{

node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key={0}]", key)));

doc.Save(getConfigFilePath());

}

}

catch (NullReferenceException e)

{

throw new Exception(string.Format("The key {0} does not exist.", key), e);

}

}

private static XmlDocument loadConfigDocument()

{

XmlDocument doc = null;

try

{

doc = new XmlDocument();

doc.Load(getConfigFilePath());

return doc;

}

catch (System.IO.FileNotFoundException e)

{

throw new Exception("No configuration file found.", e);

}

}

private static string getConfigFilePath()

{

return Assembly.GetExecutingAssembly().Location + ".config";

}

}

هذا الموضوع مغلق.

مواضيع مشابهة