黑马程序员技术交流社区

标题: C#访问配置文件 [打印本页]

作者: sunrise2    时间: 2014-7-23 23:42
标题: C#访问配置文件
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.IO;

  7. namespace HelloCSharp
  8. {
  9.     //[section]
  10.     //key=value
  11.     //
  12.     class IniHelper
  13.     {
  14.         /// <summary>
  15.         /// ini文件的路径
  16.         /// </summary>
  17.         private string filePath;


  18.         [DllImport("kernel32")]
  19.         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  20.         [DllImport("kernel32")]
  21.         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

  22.         public IniHelper(string filePath)
  23.         {
  24.             this.filePath = filePath;
  25.         }

  26.         public void IniWriteValue(string section, string key, string val)
  27.         {
  28.             WritePrivateProfileString(section, key, val, filePath);
  29.         }

  30.         public string IniReadValue(string section, string key)
  31.         {
  32.             StringBuilder temp = new StringBuilder(500);
  33.             GetPrivateProfileString(section, key, "", temp, 500, filePath);
  34.             return temp.ToString();
  35.         }
  36.     }
  37. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2