C#アクションiniファイル

4610 ワード

   public class IniOperation

    {

        private static string iniFilePath;  //ini 

        const int MAXENTRY = 500000;               // 500*1000 

        public IniOperation(string strIniFileName)

        {

            if (strIniFileName.Substring(2, 1) == "\\")// +Iin , 

            { 

                iniFilePath = strIniFileName; 

            }

            else if (strIniFileName.Contains(".ini"))

            {

                string serciecName = AppconfigClass.GetValue("ServiceName");

                iniFilePath = string.IsNullOrEmpty(serciecName) ? strIniFileName : CommonClass.GetWindowsServiceInstallPath(serciecName) + strIniFileName;

            }

            else //i18n

            {

                iniFilePath = HttpContext.Current.Request.PhysicalApplicationPath + strIniFileName;

            }

        }



        #region  INI 

        /// <summary>

        ///  INI 

        /// </summary>

        /// <param name="IniKey"></param>

        /// <returns></returns>

        public string IniValue(string Section, string IniKey)

        {

            if (System.IO.File.Exists(iniFilePath))

            {

                System.IO.File.GetAccessControl(iniFilePath);

                StringBuilder temp = new StringBuilder(500);

                while (true)

                {

                    try

                    {

                        GetPrivateProfileString(Section, IniKey, "", temp, 500, iniFilePath);

                    }

                    catch (Exception ex)

                    {



                        System.Threading.Thread.Sleep(50);

                        break;

                    }

                    break;

                }

                return temp.ToString();

            }

            else

                return string.Empty;

        }

        /// <summary> 

        ///  INI  

        /// </summary> 

        /// <param name="Section"> (  [TypeName] )</param> 

        /// <param name="Key"> </param> 

        /// <param name="Value"> </param> 

        public void IniWriteValue(string Section, string Key, string Value)

        {

            if (System.IO.File.Exists(iniFilePath))

            {

                while (true)

                {

                    try

                    {

                        WritePrivateProfileString(Section, Key, Value, iniFilePath);

                    }

                    catch

                    {

                        System.Threading.Thread.Sleep(50);

                        return;

                    }

                    return;

                }

            }

            else

                return;

        }



        public string[] GetSectionNames()

        {

            if (System.IO.File.Exists(iniFilePath))

            {

                byte[] byBuff = new byte[MAXENTRY];

                GetPrivateProfileSectionNames(byBuff, MAXENTRY, iniFilePath);

                return Encoding.Default.GetString(byBuff).Trim('\0').Split('\0');

            }

            else

            {

                string[] strReturn = new string[0];

                return strReturn;

            }

        }



        [DllImport("kernel32")]

        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);



        [DllImport("kernel32")]

        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);



        [DllImport("kernel32")]

        private static extern int GetPrivateProfileSectionNames(byte[] byRetBuff, int iSize, string filePath);



        #endregion



        static public string getXmlNodeValue(string filePath, string nodeName)

        {

            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(filePath);

            XmlNodeList nodes = xmldoc.GetElementsByTagName("add");

            string strvalue = "";

            for (int i = 0; i < nodes.Count; i++)

            {

                XmlAttribute att = nodes[i].Attributes["key"];

                if (att.Value == nodeName)

                {

                    strvalue = nodes[i].Attributes["value"].Value;

                }



            }

            return strvalue;

        }



    }