转载

C#操作ini文件

C#创建ini文件、读取值、修改值

调用方法:

int iBaudRate = InIHelper.ReadConfig<int>("COM_SETUP", "波特率")
InIHelper类:
1    public class InIHelper  2     {  3         private static string FileName = Application.StartupPath + "//AppConfig.ini";  4         /// <summary>  5         /// 读取配置文件  6         /// </summary>  7         /// <typeparam name="T"></typeparam>  8         /// <param name="section"></param>  9         /// <param name="key"></param> 10         /// <returns></returns> 11         public static T ReadConfig<T>(string section, string key) 12         { 13             if (File.Exists(FileName)) 14             { 15                 IniFile f = new IniFile(FileName); 16                 string value = f.ReadContentValue(section, key); 17  18                 if (String.IsNullOrWhiteSpace(value)) 19                     return default(T); 20  21                 if (typeof(T).IsEnum) 22                     return (T)Enum.Parse(typeof(T), value, true); 23  24                 return (T)Convert.ChangeType(value, typeof(T)); 25             } 26             else 27             { 28                 return default(T); 29             } 30         } 31  32         /// <summary> 33         /// 写配置文件 34         /// </summary> 35         /// <param name="section"></param> 36         /// <param name="key"></param> 37         /// <param name="value"></param> 38         public static void WriteConfig(string section, string key, string value) 39         { 40             //如果文件不存在,则创建 41             if (!File.Exists(FileName)) 42             { 43                 using (FileStream myFs = new FileStream(FileName, FileMode.Create)) { } 44             } 45  46             IniFile f = new IniFile(FileName); 47             f.WriteContentValue(section, key, value); 48         } 49     }

核心代码:

public class IniFile {  public string Path;  public IniFile(string path)  {   this.Path = path;  }  /// <summary>  /// 写入INI文件  /// </summary>  /// <param name="section">节点名称[如[TypeName]]</param>  /// <param name="key">键</param>  /// <param name="val">值</param>  /// <param name="filepath">文件路径</param>  /// <returns></returns>  [DllImport("kernel32")]  private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);  /// <summary>  /// 读取INI文件  /// </summary>  /// <param name="section">节点名称</param>  /// <param name="key">键</param>  /// <param name="def">值</param>  /// <param name="retval">stringbulider对象</param>  /// <param name="size">字节大小</param>  /// <param name="filePath">文件路径</param>  /// <returns></returns>  [DllImport("kernel32")]  private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);  /// <summary>  /// 写入  /// </summary>  /// <param name="section"></param>  /// <param name="key"></param>  /// <param name="iValue"></param>  public void WriteContentValue(string section, string key, string iValue)  {   WritePrivateProfileString(section, key, iValue, this.Path);  }  /// <summary>  /// 读取INI文件中的内容方法  /// </summary>  /// <param name="Section">键</param>  /// <param name="key">值</param>  /// <returns></returns>  public string ReadContentValue(string Section, string key)  {   StringBuilder temp = new StringBuilder(1024);   GetPrivateProfileString(Section, key, "", temp, 1024, this.Path);   return temp.ToString();  } } 
正文到此结束
Loading...