Pioneers
پنج شنبه 10 اسفند 1385, 00:25 صبح
برای کار با آیتمهای فایل پیکربندی, از کد زیر استفاده میشود:
System.Configuration.ConfigurationSettings.AppSett ings
اما این روش مشکلاتی دارد مثل عدم امکان حذف یا تغییر...
احتمالا بهترین راه اینست که فایل پیکربندی را بصورت فایل 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);
}
در نهایت فایل را در محل اصلی ذخیره میکنیم که در وب و ویندوز محل متفاوتی دارند(البته مجوز 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
}
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
}
متد 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;
}
تذکر 1: کدها کاملا گویاست.
تذکر 2: منبع http://www.CodeProject.com
تذکر 3:خوشحال میشم نظر بدین.
System.Configuration.ConfigurationSettings.AppSett ings
اما این روش مشکلاتی دارد مثل عدم امکان حذف یا تغییر...
احتمالا بهترین راه اینست که فایل پیکربندی را بصورت فایل 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);
}
در نهایت فایل را در محل اصلی ذخیره میکنیم که در وب و ویندوز محل متفاوتی دارند(البته مجوز 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
}
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
}
متد 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;
}
تذکر 1: کدها کاملا گویاست.
تذکر 2: منبع http://www.CodeProject.com
تذکر 3:خوشحال میشم نظر بدین.