修改APP.Config內容_2
參考下列資料
.NET 2.0中,配置文件app.config的读写(VS2005,C#)
再進一步改寫
//新增更改app.Config的值
/// <summary>
/// Opts the config.
/// </summary>
/// <param name="AppKey">The app key.</param>
/// <param name="AppValue">The app value.</param>
/// <param name="ExeFileName">Name of the exe file.</param>
public static void OptConfig(string AppKey, string AppValue, string ExeFileName)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + ExeFileName +".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_strFullPath);
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Removes the config.
/// </summary>
/// <param name="AppKey">The app key.</param>
/// <param name="ExeFileName">Name of the exe file.</param>
public static void RemoveConfig(string AppKey, string ExeFileName)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + ExeFileName + ".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_strFullPath);
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null)
xNode.RemoveChild(xNode.SelectSingleNode("//add[@key='" + AppKey + "']"));
xDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Removes all config.
/// </summary>
/// <param name="ExeFileName">Name of the exe file.</param>
public static void RemoveAllConfig(string ExeFileName)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + ExeFileName + ".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_strFullPath);
XmlNode xNode;
xNode = xDoc.SelectSingleNode("//appSettings");
xNode.RemoveAll();
xDoc.Save(m_strFullPath);
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Reads all config.
/// </summary>
/// <param name="ExeFileName">Name of the exe file.</param>
/// <returns></returns>
public static List<string> ReadAllConfig(string ExeFileName)
{
try
{
List<string> result = new List<string>();
Assembly Asm = Assembly.GetExecutingAssembly();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + ExeFileName + ".config";
FileInfo fi = new FileInfo(m_strFullPath);
StreamReader sr = fi.OpenText();
while (sr.Peek() >= 0)
{
string temp = sr.ReadLine();
if (temp.IndexOf("add") != -1)
{
string[] temp2 = temp.Split('"');
result.Add("AppKey:" + temp2[1]);
result.Add("AppValue:" + temp2[3]);
}
}
sr.Close();
return result;
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Displays the useage config.
/// </summary>
/// <param name="AppKey">The app key.</param>
/// <param name="ExeFileName">Name of the exe file.</param>
/// <returns></returns>
public static string DisplayUseageConfig(string AppKey,string ExeFileName)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
string m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + ExeFileName +".config";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(m_strFullPath);
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
return (xElem1 != null) ? "該名稱已使用":"該名稱未使用";
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion