برای کار با آیتمهای فایل پیکربندی, از کد زیر استفاده میشود: کد: System.Configuration.ConfigurationSettings.AppSett ings</pre> اما این روش مشکلاتی دارد مثل عدم امکان حذف یا تغییر... احتمالا بهترین راه اینست که فایل پیکربندی را بصورت فایل XML باز کرده تغییر داده و نهایتا ذخیره کنیم. در اینجا کد های لازم برای عملیات یاد شده را میبینیم(البته بدون جزئیات نامربوط به موضوع اصلی): 1-افزون نود: کد: public void AddKey(string strKey, string strValue) { XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings"); if (KeyExists(strKey)) throw new ArgumentException("Key name: <" + strKey + "> already exists."); XmlNode newChild = appSettingsNode.FirstChild.Clone();//Structure Copy newChild.Attributes["key"].Value = strKey; newChild.Attributes["value"].Value = strValue; appSettingsNode.AppendChild(newChild); }</pre> در نهایت فایل را در محل اصلی ذخیره میکنیم که در وب و ویندوز محل متفاوتی دارند(البته مجوز rewrite باید داشته باشیم). 2-تغییر مقدار: کد: public void UpdateKey(string strKey, string newValue) { if (!KeyExists(strKey)) throw new ArgumentNullException("Key", "<" + strKey + "> does not exist."); XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings"); // Attempt to locate the requested setting. foreach (XmlNode childNode in appSettingsNode) { if (childNode.Attributes["key"].Value == strKey) childNode.Attributes["value"].Value = newValue; } //Save }</pre> 3-حذف نود: کد: // Deletes a key from the App.config public void DeleteKey(string strKey) { if (!KeyExists(strKey)) throw new ArgumentNullException("Key", "<" + strKey + "> does not exist."); XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings"); // Attempt to locate the requested setting. foreach (XmlNode childNode in appSettingsNode) { if (childNode.Attributes["key"].Value == strKey) appSettingsNode.RemoveChild(childNode); } //save }</pre> متد KeyExist: کد: public bool KeyExists(string strKey) { XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings"); // Attempt to locate the requested setting. foreach (XmlNode childNode in appSettingsNode) { if (childNode.Attributes["key"].Value == strKey) return true; } return false; }</pre>