C#读写ini配置文件 class INIFile { private string fileName; public INIFile(string fileName) { this.fileName = fileName; } public void Write(string section, string key, string value) { WritePrivateProfileString(section, key, value, fileName); } public string Read(string section, string key) { StringBuilder stringBuilder = new StringBuilder(260); GetPrivateProfileString(section, key, "", stringBuilder, 260, fileName); return stringBuilder.ToString(); } [DllImport("kernel32")] //参数说明: section ini文件的字段名,key ini文件的section下的变量名,value 键值即变量值(string型),fileName ini文件的完整名(包括路径) private static extern long WritePrivateProfileString(string section, string key, string value, string fileName); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder returnedValue, int returnedMaxSize, string fileName); }