using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Win32;
using System.Drawing;
using System.Windows.Forms;
namespace ChartFormat
{
class Config
{
#region 刪除配置文件
/// <summary>
/// 刪除配置文件
/// </summary>
/// <returns></returns>
public static bool DelConfig()
{
string m_strFullPath = "";
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
try
{
File.Delete(m_strFullPath);
return true;
}
catch
{
return false;
}
}
#endregion
#region 判斷該路徑文件是否存在 如果不存在則創建
/// <summary>
/// 判斷該路徑文件是否存在 如果不存在則創建
/// </summary>
/// <param name="m_strFullPath">路徑</param>
public static void ConfigFile(string m_strFullPath)
{
StreamWriter sr = null;
if (!System.IO.File.Exists(m_strFullPath))
{
try
{
sr = new StreamWriter(m_strFullPath);
sr.WriteLine("<?xml version=" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "utf-8" + "\"" + " ?>");
sr.WriteLine("<configuration>");
sr.WriteLine("<appSettings>");
sr.WriteLine("<add key=" + "\"" + "NameColour" + "\"" + " value=" + "\"" + "Blue" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "InfoColour" + "\"" + " value=" + "\"" + "Black" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsURL" + "\"" + " value=" + "\"" + "0" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsEmail" + "\"" + " value=" + "\"" + "0" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsMoveTime" + "\"" + " value=" + "\"" + "1" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "IsMove" + "\"" + " value=" + "\"" + "1" + "\"" + "/>");
sr.WriteLine("<add key=" + "\"" + "MoveValue" + "\"" + " value=" + "\"" + "3" + "\"" + "/>");
sr.WriteLine("</appSettings>");
sr.WriteLine("</configuration>");
sr.Close();
}
catch (Exception e)
{
sr.Close();
MessageBox.Show("創建配置文件錯誤!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
}
#endregion
#region 更新Config文件
/// <summary>
/// 更新Config文件
/// </summary>
/// <param name="p_strKey">鍵</param>
/// <param name="p_strValue">值</param>
public static void UpdateConfig(string p_strKey, string p_strValue)
{
try
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
ConfigFile(m_strFullPath);
if (File.Exists(m_strFullPath))
{
//處理數據文件為只讀的情況
if ((File.GetAttributes(m_strFullPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(m_strFullPath, File.GetAttributes(m_strFullPath) & (~FileAttributes.ReadOnly));
}
}
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍歷所有子節點
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
{
xe.SetAttribute("value", p_strValue);
}
}
xmlDoc.Save(m_strFullPath);
//fs.Close();
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
public class ConfigInfo
{
private string strNameColor =ColorTranslator.ToHtml( System.Drawing.Color.Blue);
private string strInfoColor = ColorTranslator.ToHtml(System.Drawing.Color.Black);
private string strIsURL = "1";
private string strIsEmail = "1";
private string strIsMove = "1";
private string strMoveValue = "3";
private string strIsMoveTime = "1";
/// <summary>
/// 是否移除空格
/// </summary>
public bool IsMoveTime
{
get
{
if (strIsMoveTime == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsMoveTime = "1";
}
else
{
strIsMoveTime = "0";
}
}
}
/// <summary>
/// 縮進空格
/// </summary>
public decimal MoveValue
{
get
{
return Convert.ToDecimal(strMoveValue);
}
set
{
strMoveValue = value.ToString();
}
}
/// <summary>
/// 是否縮進
/// </summary>
public bool IsMove
{
get
{
if (strIsMove == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsMove = "1";
}
else
{
strIsMove = "0";
}
}
}
/// <summary>
/// 郵件是否可以點擊
/// </summary>
public bool IsEmail
{
get
{
if (strIsEmail == "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsEmail = "1";
}
else
{
strIsEmail = "0";
}
}
}
/// <summary>
/// 網頁是否可以點擊
/// </summary>
public bool IsURL
{
get
{
if (strIsURL== "1")
{
return true;
}
else
{
return false;
}
}
set
{
if (value)
{
strIsURL = "1";
}
else
{
strIsURL = "0";
}
}
}
/// <summary>
/// 消息顏色
/// </summary>
public Color InfoColor
{
get
{
return ColorTranslator.FromHtml(strInfoColor);
}
set
{
strInfoColor = ColorTranslator.ToHtml(value);
}
}
/// <summary>
/// 信息顏色
/// </summary>
public Color NameColor
{
get
{
return ColorTranslator.FromHtml(strNameColor); ;
}
set
{
strNameColor = ColorTranslator.ToHtml(value);
}
}
public ConfigInfo()
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn in nodeList)//遍歷所有子節點
{
XmlNodeReader objNdRd = new XmlNodeReader(xn);
while (objNdRd.Read())
{
if (objNdRd.GetAttribute("key").IndexOf("NameColour") != -1)
{
this.strNameColor = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("InfoColour") != -1)
{
this.strInfoColor = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsURL") != -1)
{
this.strIsURL = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsEmail") != -1)
{
this.strIsEmail = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsMove") != -1)
{
this.strIsMove = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("MoveValue") != -1)
{
this.strMoveValue = objNdRd.GetAttribute("value");
}
}
}
}
public void Update()
{
Config.DelConfig();
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
Config.UpdateConfig("NameColour", strNameColor);
Config.UpdateConfig("InfoColour", strInfoColor);
Config.UpdateConfig("IsURL", strIsURL);
Config.UpdateConfig("IsEmail", strIsEmail);
Config.UpdateConfig("IsMove", strIsMove);
Config.UpdateConfig("MoveValue", strMoveValue);
}
}
}