在做项目过程中,有时需要保存一些简单的配置信息,可以使用xml,也可以使用INI文件。下面是C#中读取INI的方法,相信大部分朋友都使用过这种方式。
INI文件的存储方式如下,
- [section]
- key=value
- key=value
读取写入方法,
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
-
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
-
- private static string ReadString(string section, string key, string def, string filePath)
- {
- StringBuilder temp = new StringBuilder(1024);
-
- try
- {
- GetPrivateProfileString(section, key, def, temp, 1024, filePath);
- }
- catch
- { }
- return temp.ToString();
- }
- public static string[] ReadIniAllKeys(string section,string filePath)
- {
- UInt32 MAX_BUFFER = 32767;
-
- string[] items = new string[0];
-
- IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
-
- UInt32 bytesReturned = GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, filePath);
-
- if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
- {
- string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
-
- items = returnedString.Split(new char[] { ‘\0‘ }, StringSplitOptions.RemoveEmptyEntries);
- }
-
- Marshal.FreeCoTaskMem(pReturnedString);
-
- return items;
- }
-
- public static string ReadIniKeys(string section, string keys, string filePath)
- {
- return ReadString(section, keys, "", filePath);
- }
-
- public static void WriteIniKeys(string section, string key, string value, string filePath)
- {
- WritePrivateProfileString(section, key, value, filePath);
- }
如果要删除某一项:
- WriteIniKeys(section, key, null, recordIniPath);
如上就可以读取和写入了,那么INI文件如何创建呢?
- [DllImport("kernel32")]
- public static extern long WritePrivateProfileString(string section, string key, string value, string iniPath);
调用该方法,即可创建你的ini文件和想要保存的值。
当然上面的ini操作并不是很详细的,以下从http://blog.csdn.net/sdfkfkd/article/details/7050733的博客转载的一片描述INI操作的,比较详细,值得学习。
- public class INIOperationClass
- {
-
- #region INI文件操作
-
-
-
- #region API声明
-
-
-
-
-
-
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, uint nSize, string lpFileName);
-
-
-
-
-
-
-
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
-
-
-
-
-
-
-
-
-
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, [In, Out] char[] lpReturnedString, uint nSize, string lpFileName);
-
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, uint nSize, string lpFileName);
-
-
-
-
-
-
-
-
-
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool WritePrivateProfileSection(string lpAppName, string lpString, string lpFileName);
-
-
-
-
-
-
-
-
-
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
-
- #endregion
-
- #region 封装
-
-
-
-
-
-
- public static string[] INIGetAllSectionNames(string iniFile)
- {
- uint MAX_BUFFER = 32767;
-
- string[] sections = new string[0];
-
-
- IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
- uint bytesReturned = INIOperationClass.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);
- if (bytesReturned != 0)
- {
-
- string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();
-
-
- sections = local.Split(new char[] { ‘\0‘ }, StringSplitOptions.RemoveEmptyEntries);
- }
-
-
- Marshal.FreeCoTaskMem(pReturnedString);
-
- return sections;
- }
-
-
-
-
-
-
-
- public static string[] INIGetAllItems(string iniFile, string section)
- {
-
- uint MAX_BUFFER = 32767;
-
- string[] items = new string[0];
-
-
- IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
-
- uint bytesReturned = INIOperationClass.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);
-
- if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
- {
-
- string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
- items = returnedString.Split(new char[] { ‘\0‘ }, StringSplitOptions.RemoveEmptyEntries);
- }
-
- Marshal.FreeCoTaskMem(pReturnedString);
-
- return items;
- }
-
-
-
-
-
-
-
- public static string[] INIGetAllItemKeys(string iniFile, string section)
- {
- string[] value = new string[0];
- const int SIZE = 1024 * 10;
-
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- char[] chars = new char[SIZE];
- uint bytesReturned = INIOperationClass.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);
-
- if (bytesReturned != 0)
- {
- value = new string(chars).Split(new char[] { ‘\0‘ }, StringSplitOptions.RemoveEmptyEntries);
- }
- chars = null;
-
- return value;
- }
-
-
-
-
-
-
-
-
-
- public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue)
- {
- string value = defaultValue;
- const int SIZE = 1024 * 10;
-
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- if (string.IsNullOrEmpty(key))
- {
- throw new ArgumentException("必须指定键名称(key)", "key");
- }
-
- StringBuilder sb = new StringBuilder(SIZE);
- uint bytesReturned = INIOperationClass.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile);
-
- if (bytesReturned != 0)
- {
- value = sb.ToString();
- }
- sb = null;
-
- return value;
- }
-
-
-
-
-
-
-
-
- public static bool INIWriteItems(string iniFile, string section, string items)
- {
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- if (string.IsNullOrEmpty(items))
- {
- throw new ArgumentException("必须指定键值对", "items");
- }
-
- return INIOperationClass.WritePrivateProfileSection(section, items, iniFile);
- }
-
-
-
-
-
-
-
-
-
- public static bool INIWriteValue(string iniFile, string section, string key, string value)
- {
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- if (string.IsNullOrEmpty(key))
- {
- throw new ArgumentException("必须指定键名称", "key");
- }
-
- if (value == null)
- {
- throw new ArgumentException("值不能为null", "value");
- }
-
- return INIOperationClass.WritePrivateProfileString(section, key, value, iniFile);
-
- }
-
-
-
-
-
-
-
-
- public static bool INIDeleteKey(string iniFile, string section, string key)
- {
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- if (string.IsNullOrEmpty(key))
- {
- throw new ArgumentException("必须指定键名称", "key");
- }
-
- return INIOperationClass.WritePrivateProfileString(section, key, null, iniFile);
- }
-
-
-
-
-
-
-
- public static bool INIDeleteSection(string iniFile, string section)
- {
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- return INIOperationClass.WritePrivateProfileString(section, null, null, iniFile);
- }
-
-
-
-
-
-
-
- public static bool INIEmptySection(string iniFile, string section)
- {
- if (string.IsNullOrEmpty(section))
- {
- throw new ArgumentException("必须指定节点名称", "section");
- }
-
- return INIOperationClass.WritePrivateProfileSection(section, string.Empty, iniFile);
- }
-
-
- private void TestIniINIOperation()
- {
-
- string file = "F:\\TestIni.ini";
-
-
- INIWriteValue(file, "Desktop", "Color", "Red");
- INIWriteValue(file, "Desktop", "Width", "3270");
-
- INIWriteValue(file, "Toolbar", "Items", "Save,Delete,Open");
- INIWriteValue(file, "Toolbar", "Dock", "True");
-
-
- INIWriteItems(file, "Menu", "File=文件\0View=视图\0Edit=编辑");
-
-
- string[] sections = INIGetAllSectionNames(file);
-
-
- string[] items = INIGetAllItems(file, "Menu");
-
-
- string[] keys = INIGetAllItemKeys(file, "Menu");
-
-
- string value = INIGetStringValue(file, "Desktop", "color", null);
-
-
- INIDeleteKey(file, "desktop", "color");
-
-
- INIDeleteSection(file, "desktop");
-
-
- INIEmptySection(file, "toolbar");
-
- }
- #endregion
-
- #endregion
- }
C#如何读写和创建INI文件
原文:http://www.cnblogs.com/lvdongjie/p/5433156.html