MachineConfiguration.cs 4.4 KB
using System;
using System.IO;
using System.Xml;

using System.Windows.Forms;

namespace ServiceManager
{
	/// <summary>
	/// MachineConfiguration 的摘要说明。
	/// </summary>
	public class MachineConfiguration
	{
		private string _MachineConfigPath;
		public const string SYSTEMWEB = @"system.web";
		public MachineConfiguration()
		{
			Initialize();
		}

		/// <summary>
		/// 初始化
		/// </summary>
		public void Initialize()
		{
			string f_SystemFolder = "";
			try
			{
				f_SystemFolder = Environment.SystemDirectory;
				f_SystemFolder = f_SystemFolder.Substring(0,f_SystemFolder.LastIndexOf(@"\"));
				f_SystemFolder += @"\Microsoft.NET\Framework\v";
				f_SystemFolder += Environment.Version.ToString();
				f_SystemFolder = f_SystemFolder.Substring(0,f_SystemFolder.LastIndexOf(@"."));
				f_SystemFolder += @"\Config\machine.config";
			}
			catch
			{
				_MachineConfigPath = "";
			}
			_MachineConfigPath = f_SystemFolder;
		}

		/// <summary>
		/// 设置<configuration/><system.web/><processModel/>节点userName属性设为"System"
		/// </summary>
		/// <returns>操作成功 返回true,操作失败 返回false</returns>
		public bool ProcessModelSetUserName()
		{
			string f_NodeName = "processModel";
			string f_NodeKey = "userName";
			string f_NodeValue = "System";
            if (ModifyConfigSetting(MachineConfiguration.SYSTEMWEB, f_NodeName, f_NodeKey, f_NodeValue))
                return true;
            else
                return false;

		}

		/// <summary>
		/// 实现对machine节点的属性值修改的封装
		/// </summary>
		/// <param name="ConfigNode">设置文件主节点</param>
		/// <param name="NodeName">设置节点</param>
		/// <param name="NodeKey">键标识</param>
		/// <param name="NodeValue">键值</param>
		/// <returns>操作成功 返回true,操作失败 返回false</returns>
		public bool ModifyConfigSetting(string ConfigNode,string NodeName,string NodeKey,string NodeValue)
		{
			if(!File.Exists(_MachineConfigPath))
			{                
				return false;
			}
			XmlDocument doc = new XmlDocument();
			try
			{
				//确保 config 文件可写	Benjamin Qin 2003-11-13
				FileAttributes m_fa = File.GetAttributes(_MachineConfigPath);
				if((m_fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)	//若为只读
					File.SetAttributes(_MachineConfigPath,m_fa^FileAttributes.ReadOnly);//则设为非只读
				if((m_fa & FileAttributes.Hidden) == FileAttributes.Hidden)	//若为隐藏
					File.SetAttributes(_MachineConfigPath,m_fa^FileAttributes.Hidden);//则设为非隐藏
				doc.Load(_MachineConfigPath);                
			}
			catch
			{
				return false;
			}
			XmlNode node = doc.DocumentElement;
			string f_xPath = "";
            
            try
            {
                f_xPath = "descendant::" + ConfigNode;
                //node = node.SelectSingleNode(f_xPath);

                //f_xPath = ConfigNode;//"descendant::" + ;
                //node = node.SelectSingleNode(f_xPath);
                if (node == null)
                {                    
                    return false;
                }
                else
                {
                    f_xPath = "descendant::" + NodeName;
                    node = node.SelectSingleNode(f_xPath);
                    
                    if (node == null)
                    {
                        return false;
                    }
                    else
                    {                        
                        try
                        {
                            //node.Attributes[NodeKey].Value = NodeValue;
                        }
                        catch
                        {
                            //MessageBox.Show("7" + ConfigNode.ToString() + " " + NodeKey.ToString() + " " + NodeName.ToString() + " " + NodeValue.ToString());
                            return false;
                        }
                    }
                }
                try
                {
                    doc.Save(_MachineConfigPath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return false;
                }

                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
		}

		/// <summary>
		/// Machine.config路径
		/// </summary>
		public string MachineConfigPath
		{
			get
			{
				return _MachineConfigPath;
			}
			set
			{
				_MachineConfigPath = value;
			}
		}
	}
}