[C#] C# 操作INI配置文件代码演示详解 →→→→→进入此内容的聊天室

来自 , 2020-12-30, 写在 C#, 查看 141 次.
URL http://www.code666.cn/view/d117dca1
  1.  
  2. #region 调用windowsAPI  
  3.        //用来读取INI 文件的内容  
  4.        [DllImport("Kernel32.dll")]  
  5.        private static extern int GetPrivateProfileString(string strAppName,string strKeyName,string strDefault,StringBuilder sbReturnString,int nSize,string strFileName);  
  6.        //返回所读取的字符串值的真实长度  
  7.        [DllImport("Kernel32.dll")]  
  8.        private extern static int GetPrivateProfileStringA(string strAppName, string strKeyName, string sDefault, byte[] buffer, int nSize, string strFileName);  
  9.        [DllImport("Kernel32.dll")]  
  10.        private static extern int GetPrivateProfileInt(string strAppName, string strKeyName, int nDefault, string strFileName);  
  11.        //获取ini文件所有的section  
  12.        [DllImport("Kernel32.dll")]  
  13.        private extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);  
  14.        //获取指定Section的key和value          
  15.        [System.Runtime.InteropServices.DllImport("Kernel32.dll")]  
  16.        private static extern int GetPrivateProfileSection(string lpAppName,byte[] lpReturnedString, int nSize,string lpFileName);  
  17.        //根据传入参数的不同进行写入或修改或删除操作(返回值 Long,非零表示成功,零表示失败)  
  18.        [DllImport("Kernel32.dll")]  
  19.        public static extern long WritePrivateProfileString(string strAppName, string strKeyName, string strKeyValue, string strFileName);  
  20.        //添加一个section内容列表  
  21.        [DllImport("Kernel32.dll")]  
  22.        public static extern long WritePrivateProfileSection(string strAppName, string strkeyandvalue, string strFileName);  
  23.        #endregion  
  24.  
  25.  
  26. 调用这些函数的所用到的方法:
  27. [html] view plaincopy
  28. #region 供UI调用的方法  
  29.  
  30.         /// <summary>  
  31.         /// 判断该ini文件是否存在如果不存在新建一个该文件  
  32.         /// </summary>  
  33.         public void FileExists()  
  34.         {  
  35.             try  
  36.             {  
  37.                 if (!File.Exists(this.filePath))  
  38.                 {  
  39.                     using (FileStream fs = File.Create(this.filePath))  
  40.                     {  
  41.                         fs.Close();  
  42.                     }  
  43.                 }  
  44.              }  
  45.             catch(Exception e)  
  46.             {  
  47.  
  48.             }  
  49.         }  
  50.  
  51.         /// <summary>  
  52.         /// 返回该配置文件中所有Section名称的集合  
  53.         /// </summary>  
  54.         /// <returns></returns>  
  55.         public ArrayList ReadSections()  
  56.         {  
  57.             byte[] buffer = new byte[65535];  
  58.             int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), this.filePath);  
  59.             int iCnt, iPos;  
  60.             ArrayList arrayList = new ArrayList();  
  61.             string tmp;  
  62.             if (rel > 0)  
  63.             {  
  64.                 iCnt = 0; iPos = 0;  
  65.                 for (iCnt = 0; iCnt < rel; iCnt++)  
  66.                 {  
  67.                     if (buffer[iCnt] == 0x00)  
  68.                     {  
  69.                         tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
  70.                         iPos = iCnt + 1;  
  71.                         if (tmp != "")  
  72.                             arrayList.Add(tmp);  
  73.                     }  
  74.                 }  
  75.             }  
  76.             return arrayList;  
  77.         }  
  78.          
  79.         /// <summary>  
  80.         ///  获取指定节点的所有KEY的名称  
  81.         /// </summary>  
  82.         /// <param name="sectionName"></param>  
  83.         /// <returns></returns>  
  84.         public ArrayList ReadKeys(string sectionName)  
  85.         {  
  86.  
  87.             byte[] buffer = new byte[5120];  
  88.             int rel = GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), this.filePath);  
  89.  
  90.             int iCnt, iPos;  
  91.             ArrayList arrayList = new ArrayList();  
  92.             string tmp;  
  93.             if (rel > 0)  
  94.             {  
  95.                 iCnt = 0; iPos = 0;  
  96.                 for (iCnt = 0; iCnt < rel; iCnt++)  
  97.                 {  
  98.                     if (buffer[iCnt] == 0x00)  
  99.                     {  
  100.                         tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
  101.                         iPos = iCnt + 1;  
  102.                         if (tmp != "")  
  103.                             arrayList.Add(tmp);  
  104.                     }  
  105.                 }  
  106.             }  
  107.             return arrayList;  
  108.         }  
  109.  
  110.         /// <summary>  
  111.         /// 读取指定节点下的指定key的value返回string  
  112.         /// </summary>  
  113.         /// <param name="section"></param>  
  114.         /// <param name="key"></param>  
  115.         /// <returns></returns>  
  116.         public string GetIniKeyValueForStr(string section, string key)  
  117.         {  
  118.             if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return string.Empty;  
  119.             StringBuilder strTemp = new StringBuilder(256);  
  120.             GetPrivateProfileString(section, key, string.Empty, strTemp, 256, this.filePath);  
  121.             return strTemp.ToString().Trim();  
  122.         }  
  123.  
  124.         /// <summary>  
  125.         /// 从指定的节点中获取一个整数值( Long,找到的key的值;如指定的key未找到,就返回默认值。如找到的数字不是一个合法的整数,函数会返回其中合法的一部分。如,对于“xyz=55zz”这个条目,函数返回55。)  
  126.         /// </summary>  
  127.         /// <param name="section"></param>  
  128.         /// <param name="key"></param>  
  129.         /// <returns></returns>  
  130.         public int GetIniKeyValueForInt(string section, string key)  
  131.         {  
  132.             if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return 0;  
  133.             return GetPrivateProfileInt(section, key, 0, this.filePath);  
  134.         }  
  135.  
  136.         /// <summary>  
  137.         /// 读取指定节点下的所有key 和value  
  138.         /// </summary>  
  139.         /// <param name="section"></param>  
  140.         /// <returns></returns>  
  141.         public ArrayList GetIniSectionValue(string section)  
  142.         {  
  143.             byte[] buffer = new byte[5120];  
  144.             int rel = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), this.filePath);  
  145.  
  146.             int iCnt, iPos;  
  147.             ArrayList arrayList = new ArrayList();  
  148.             string tmp;  
  149.             if (rel > 0)  
  150.             {  
  151.                 iCnt = 0; iPos = 0;  
  152.                 for (iCnt = 0; iCnt < rel; iCnt++)  
  153.                 {  
  154.                     if (buffer[iCnt] == 0x00)  
  155.                     {  
  156.                         tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
  157.                         iPos = iCnt + 1;  
  158.                         if (tmp != "")  
  159.                             arrayList.Add(tmp);  
  160.                     }  
  161.                 }  
  162.             }  
  163.             return arrayList;  
  164.         }  
  165.  
  166.         /// <summary>  
  167.         /// 往指定section的key中写入value  
  168.         /// </summary>  
  169.         /// <param name="section"></param>  
  170.         /// <param name="key"></param>  
  171.         /// <param name="value"></param>  
  172.         /// <returns></returns>  
  173.         public bool WriteIniKey(string section, string key, string value)  
  174.         {  
  175.             try  
  176.             {  
  177.                 if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)  
  178.                 {  
  179.                     flag = false;  
  180.                 }  
  181.                 else  
  182.                 {  
  183.                    
  184.                     if (WritePrivateProfileString(section, key, value, this.filePath) == 0)  
  185.                     {  
  186.                         flag = false;  
  187.                     }  
  188.                     else  
  189.                     {  
  190.                         flag = true;  
  191.                     }  
  192.                 }  
  193.             }  
  194.             catch  
  195.             {  
  196.                 flag = false;  
  197.             }  
  198.             return flag;  
  199.         }  
  200.  
  201.         /// <summary>  
  202.         /// 修改指定section的key的值  
  203.         /// </summary>  
  204.         /// <param name="section"></param>  
  205.         /// <param name="key"></param>  
  206.         /// <param name="value"></param>  
  207.         /// <returns></returns>  
  208.         public bool EditIniKey(string section, string key, string value)  
  209.         {  
  210.             try  
  211.             {  
  212.                 if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)  
  213.                 {  
  214.                     flag = false;  
  215.                 }  
  216.                 else  
  217.                 {  
  218.                     if (WritePrivateProfileString(section, key, value, this.filePath) == 0)  
  219.                     {  
  220.                         flag = false;  
  221.                     }  
  222.                     else  
  223.                     {  
  224.                         flag = true;  
  225.                     }  
  226.                 }  
  227.             }  
  228.             catch  
  229.             {  
  230.                 flag = false;  
  231.             }  
  232.             return flag;  
  233.         }  
  234.  
  235.         /// <summary>  
  236.         /// 删除指定section的指定key  
  237.         /// </summary>  
  238.         /// <param name="section"></param>  
  239.         /// <param name="key"></param>  
  240.         /// <returns></returns>  
  241.         public bool DeleteIniKey(string section, string key)  
  242.         {  
  243.             try  
  244.             {  
  245.                 if (section.Trim().Length <= 0 || key.Trim().Length <= 0)  
  246.                 {  
  247.                     flag = false;  
  248.                 }  
  249.                 else  
  250.                 {  
  251.                     if (WritePrivateProfileString(section, key, null, this.filePath) == 0)  
  252.                     {  
  253.                         flag = false;  
  254.                     }  
  255.                     else  
  256.                     {  
  257.                         flag = true;  
  258.                     }  
  259.                 }  
  260.             }  
  261.             catch  
  262.             {  
  263.                 flag = false;  
  264.             }  
  265.             return flag;  
  266.         }  
  267.  
  268.         /// <summary>  
  269.         /// 删除指定section  
  270.         /// </summary>  
  271.         /// <param name="section"></param>  
  272.         /// <returns></returns>  
  273.         public bool DeleteIniSection(string section)  
  274.         {  
  275.             try  
  276.             {  
  277.                 if (section.Trim().Length <= 0)  
  278.                 {  
  279.                     flag = false;  
  280.                 }  
  281.                 else  
  282.                 {  
  283.                     if (WritePrivateProfileString(section, null, null, this.filePath) == 0)  
  284.                     {  
  285.                         flag = false;  
  286.                     }  
  287.                     else  
  288.                     {  
  289.                         flag = true;  
  290.                     }  
  291.                 }  
  292.             }  
  293.             catch  
  294.             {  
  295.                 flag = false;  
  296.             }  
  297.             return flag;  
  298.         }  
  299.  
  300.         /// <summary>  
  301.         /// 给一个节点写入key和value列表  
  302.         /// </summary>  
  303.         /// <param name="section"></param>  
  304.         /// <param name="ht"></param>  
  305.         /// <returns></returns>  
  306.         public bool WriteIniSectionAndValue(string section, Hashtable ht)  
  307.         {  
  308.             string lpString = "";  
  309.             try  
  310.             {  
  311.                 if (section.Trim().Length <= 0 || ht.Count == 0)  
  312.                 {  
  313.                     flag = false;  
  314.                 }  
  315.                 else  
  316.                 {  
  317.                     foreach (DictionaryEntry de in ht)  
  318.                     {  
  319.                         lpString += de.Key+"="+de.Value;                        
  320.                         lpString += "\r\n";  
  321.                     }  
  322.                     if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)  
  323.                     {  
  324.                         flag = false;  
  325.                     }  
  326.                     else  
  327.                     {  
  328.                         flag = true;  
  329.                     }  
  330.  
  331.                 }  
  332.             }  
  333.             catch  
  334.             {  
  335.                 flag = false;  
  336.             }  
  337.             return flag;  
  338.         }  
  339.          
  340.         /// <summary>  
  341.         /// 给一个节点写入key 列表  
  342.         /// </summary>  
  343.         /// <param name="section"></param>  
  344.         /// <param name="lstKeyValue"></param>  
  345.         /// <returns></returns>  
  346.         public bool WriteIniSectionName(string section, List<string> lstKeyValue)  
  347.         {  
  348.             string lpString = "";  
  349.             try  
  350.             {  
  351.                 if (section.Trim().Length <= 0 || lstKeyValue.Count == 0)  
  352.                 {  
  353.                     flag = false;  
  354.                 }  
  355.                 else  
  356.                 {  
  357.                     for (int i = 0; i < lstKeyValue.Count; ++i)  
  358.                     {  
  359.                         lpString += lstKeyValue[i];  
  360.                         lpString += "\r\n";  
  361.                     }  
  362.                     if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)  
  363.                     {  
  364.                         flag = false;  
  365.                     }  
  366.                     else  
  367.                     {  
  368.                         flag = true;  
  369.                     }  
  370.  
  371.                 }  
  372.             }  
  373.             catch  
  374.             {  
  375.                 flag = false;  
  376.             }  
  377.             return flag;  
  378.         }  
  379.         #endregion  
  380.  
  381.  
  382.  
  383. //csharp/6080

回复 "C# 操作INI配置文件代码演示详解"

这儿你可以回复上面这条便签

captcha