diff --git a/README.md b/README.md new file mode 100644 index 0000000..d2209c0 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# CSharpCommonClass +C#常用类汇总 + +1,iniFile.cs by zhangbc 2015-07-05:此类包含了ini的基本的操作,主要功能是:存取ini文件中的数据。 diff --git a/iniFile.cs b/iniFile.cs new file mode 100644 index 0000000..db8677d --- /dev/null +++ b/iniFile.cs @@ -0,0 +1,230 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; +using System.IO; + + +namespace DataPlantingForm +{ + /// + /// 操作INI文件类 + /// by zhangbc 2015-07-06 + /// + class iniFile + { + /// + /// 内存缓冲区的长度 + /// + const int DATA_SIZE = 1024; + /// + /// 默认的内存缓冲区大小 + /// + const uint MAX_BUFFER = 32767; + private string _path; + /// + /// ini文件及其路径 + /// + private string iniPath + { + get {return _path;} + set { _path = value; } + } + [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)] + public struct StringBuffer + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = DATA_SIZE)] + public string sText; + } + + #region API声明 + /// + /// 将结点信息写入ini文件 + /// + /// 节点名称 + /// key值 + /// value值 + /// ini文件及其路径 + /// 实际写入的长度 + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); + + /// + /// 读取ini文件中指定的Key值(法一) + /// + /// 节点名称 + /// key值 + /// 读取失败时的默认值 + /// 返回的value值(自定义结构型) + /// 内存缓冲区的长度 + /// ini文件及其路径 + /// 实际读取到的长度 + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + private static extern int GetPrivateProfileString(string section, string key, string def, out StringBuffer retVal, int size, string filePath); + + /// + /// 读取ini文件中指定的Key值(法二) + /// + /// 节点名称 + /// key值 + /// 读取失败时的默认值 + /// 返回的value值(字符组型) + /// 内存缓冲区的长度 + /// ini文件及其路径 + /// 实际读取到的长度 + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + private static extern uint GetPrivateProfileString(string section, string key, string def, [In,Out] char[] retVal, uint size, string filePath); + + /// + /// 获取所有的结点名称 + /// + /// 存放节点名称的内存地址,每个结点用\0间隔 + /// 内存大小 + /// ini文件及其路径 + /// 内容的实际长度,为0表示没有内容,为size-2表示内存大小不够 + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + private static extern uint GetPrivateProfileSectionNames(IntPtr szReturnBuffer, uint size, string filePath); + + /// + /// 获取某个结点下的所有的Key和Value + /// + /// 结点名称 + /// 存放节点名称的内存地址,每个结点用\0间隔 + /// 内存大小 + /// ini文件及其路径 + /// 内容的实际长度,为0表示没有内容,为size-2表示内存大小不够 + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] + private static extern uint GetPrivateProfileSection(string section,IntPtr szReturnBuffer, uint size, string filePath); + #endregion + + /// + /// 构造函数 + /// + /// 文件名及其路径 + public iniFile(string sPath) + { + this._path = sPath; + string path = iniPath.Substring(0, iniPath.LastIndexOf('\\')); + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + if (!File.Exists(_path)) + createIniFile(); + } + + /// + /// 写文件操作 + /// + /// 节点名称 + /// key值 + /// value值 + public void writeIniValue(string section,string key,string val) + { + WritePrivateProfileString(section,key,val,this._path); + } + + /// + /// 读取ini文件的所有的section结点 + /// + /// 返回结点值列表 + public string[] readiniSections() + { + string[] sections = new string[0]; + //申请内存 + IntPtr pString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); + uint byteReturned = GetPrivateProfileSectionNames(pString,MAX_BUFFER,this._path); + if(byteReturned!=0) + { + string local = Marshal.PtrToStringAuto(pString,(int)byteReturned).ToString(); + sections = local.Split(new char[]{'\0'},StringSplitOptions.RemoveEmptyEntries); + } + //释放内存 + Marshal.FreeCoTaskMem(pString); + return sections; + } + + /// + /// 读取ini文件下指定结点下Key值列表 + /// + /// 节点名 + /// 返回Key值列表 + public string[] readIniKeys(string section) + { + string[] keys = new string[0]; + const int SIZE = DATA_SIZE * 10; + + if(string.IsNullOrEmpty(section)) + { + throw new ArgumentException("必须指定节点名称","section"); + } + char[] chars = new char[SIZE]; + uint byteReturned = GetPrivateProfileString(section, null,null,chars, SIZE, this._path); + if (byteReturned != 0) + { + keys = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); + } + chars = null; + return keys; + } + + /// + /// 读取ini文件下指定key值的value + /// + /// 节点名 + /// 指定的key值 + /// value值 + public string readIniValues(string section,string key) + { + StringBuffer retVal; + GetPrivateProfileString(section,key,null,out retVal,DATA_SIZE,this._path); + return retVal.sText.Trim(); + } + + /// + /// 读取ini文件下指定values值,重载 + /// + /// 节点名 + /// 指定的key值 + /// value值 + public string readIniValues(string section, string key, string defaultVal) + { + StringBuffer retVal; + GetPrivateProfileString(section, key, null, out retVal, DATA_SIZE, this._path); + string tmp=retVal.sText.Trim(); + return tmp==""?defaultVal:tmp; + } + + /// + /// 读取ini文件下指定结点的所有条目(key=value) + /// + /// + /// + public string[] readIniKeyVals(string section) + { + string[] items = new string[0]; + //申请内存 + IntPtr pString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); + uint byteReturned = GetPrivateProfileSection(section,pString, MAX_BUFFER, this._path); + if (!(byteReturned==MAX_BUFFER-2)||(byteReturned == 0)) + { + string local = Marshal.PtrToStringAuto(pString, (int)byteReturned).ToString(); + items = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); + } + //释放内存 + Marshal.FreeCoTaskMem(pString); + return items; + } + + /// + /// 创建空文件ini + /// + public void createIniFile() + { + StreamWriter wr = File.CreateText(this._path); + wr.Write(""); + wr.Flush(); + wr.Close(); + } + } +}