using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; using Wms.Screen.Service.IService; namespace Wms.Screen.Service.Service { /// /// INI文件操作方法 /// /// creater maya /// createon 2020-09-08 public class FileService : IFileService { public string IniPath { get; set; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct STRINGBUFFER { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string szText; } //读写INI文件的API函数 [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath); /// 读取INI文件中指定的Key的值 [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); //另一种声明方式,使用 StringBuilder 作为缓冲区类型的缺点是不能接受\0字符,会将\0及其后的字符截断, //所以对于lpAppName或lpKeyName为null的情况就不适用 [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName); //再一种声明,使用string作为缓冲区的类型同char[] [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, uint nSize, string lpFileName); //类的构造函数,传递INI档案名 public FileService() { IniPath = $@"{Directory.GetCurrentDirectory()}\ZhongTian.ini"; if (!File.Exists(IniPath)) { CreateIniFile(); } } //写INI文件 public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.IniPath); } //读取INI文件指定 public string IniReadValue(string Section, string Key) { //int i; //const int SIZE = 1024 * 10; //STRINGBUFFER RetVal; //i = GetPrivateProfileString(Section, Key, null, out RetVal, SIZE, this.IniPath); //string temp = RetVal.szText; //return temp.Trim(); string value = ""; const int SIZE = 1024 * 10; StringBuilder sb = new StringBuilder(SIZE); uint bytesReturned = GetPrivateProfileString(Section, Key, "", sb, SIZE, IniPath); if (bytesReturned != 0) { value = sb.ToString(); } return value; } /// /// 创建INI文件 /// public void CreateIniFile() { StreamWriter w = File.CreateText(IniPath); w.Write(""); w.Flush(); w.Close(); } #region 读取Section下的所有Key /// /// 读取Section下的所有Key,section为ini文件中的项目,通过该函数将对应项目中的变量都提取出来保存到KeyList中 /// /// public List ReadKeyList(string Section) { List StrList = new List(); byte[] TmpBuf = new byte[40960]; int Len = GetPrivateProfileString(Section, null, null, TmpBuf, TmpBuf.Length, this.IniPath); StrList = SplitBufToStrList(TmpBuf, Len); return StrList; } private List SplitBufToStrList(byte[] Buffer, int bufLen) { List StrList = new List(); if (bufLen != 0) { int start = 0; for (int i = 0; i < bufLen; i++) { if ((Buffer[i] == 0) && ((i - start) > 0)) { string TmpStr = Encoding.GetEncoding(0).GetString(Buffer, start, i - start); StrList.Add(TmpStr); start = i + 1; } } } return StrList; } #endregion } }