| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 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
- {
- /// <summary>
- /// INI文件操作方法
- /// </summary>
- /// 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;
-
- }
- /// <summary>
- /// 创建INI文件
- /// </summary>
- public void CreateIniFile()
- {
- StreamWriter w = File.CreateText(IniPath);
- w.Write("");
- w.Flush();
- w.Close();
- }
- #region 读取Section下的所有Key
- /// <summary>
- /// 读取Section下的所有Key,section为ini文件中的项目,通过该函数将对应项目中的变量都提取出来保存到KeyList中
- /// </summary>
- /// <param name="Section"></param>
- public List<string> ReadKeyList(string Section)
- {
- List<string> StrList = new List<string>();
- byte[] TmpBuf = new byte[40960];
- int Len = GetPrivateProfileString(Section, null, null, TmpBuf, TmpBuf.Length, this.IniPath);
- StrList = SplitBufToStrList(TmpBuf, Len);
- return StrList;
- }
- private List<string> SplitBufToStrList(byte[] Buffer, int bufLen)
- {
- List<string> StrList = new List<string>();
- 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
- }
- }
|