C# 调用系统C++接口代码 实现文件分组的读取 (暂不支持注销功能)

 public class IniFileHelper
        {

            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuffer, int size, string filePath);

            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string sectionName, string key, string value, string filePath);

            [DllImport("kernel32.dll")]
            private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);


            public static string GetValue(string sectionName, string key, string filePath)
            {
                filePath = GetAsbPath(filePath);
                byte[] array = new byte[2048];
                int btsNu = GetPrivateProfileString(sectionName, key, string.Empty, array, array.Length, filePath);
                return Encoding.Default.GetString(array, 0, btsNu);
            }

            /// <summary>
            /// 获取配置文件中的所有分组和键值对
            /// </summary>
            /// <param name="filePath"></param>
            /// <returns>如果读取数据失败返回null</returns>
            public static Dictionary<string, List<ValueTuple<string, string>>> GetAllValues(string filePath)
            {
                //1.获取绝对路径
                filePath = GetAsbPath(filePath);
                //2.根据路径读取数据,并判断读取数据是否成功
                if (!File.Exists(filePath))
                {
                    return null;
                }
                //3. 获取的到的字符串
                string readStr = string.Empty;
                try
                {
                    readStr = File.ReadAllText(filePath, Encoding.Default);
                }
                catch
                {
                    return null;
                }
                if (string.IsNullOrEmpty(readStr) || !readStr.Contains("\r\n"))
                {
                    return null;
                }
                //4. 分割字符串
                string[] readStrSplicArr = readStr.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                Dictionary<string, List<ValueTuple<string, string>>> resultDic = new Dictionary<string, List<(string, string)>>();

                string groupName = string.Empty;
                List<ValueTuple<string, string>> val = null;
                string[] valArr = null;


                foreach (string itemStr in readStrSplicArr)
                {
                    //4.1 判断是否是组名称, 特点是否包含等号
                    if (!itemStr.Contains("="))
                    {
                        if (valArr != null && !resultDic.ContainsKey(groupName))
                        {
                            resultDic.Add(groupName, val);
                        }
                        groupName = itemStr.Trim(']').Trim('[');
                        val = new List<(string, string)>();
                    }
                    else
                    {
                        valArr = itemStr.Split('=');
                        if (valArr.Length >= 1)
                        {
                            val.Add((valArr[0], valArr[1]));
                        }
                    }
                }
                if (val != null && val.Count > 0)
                {
                    resultDic.Add(groupName, val);
                    groupName = string.Empty;
                    val = null;
                }
                return resultDic;
            }

            /// <summary>
            /// 获取某个分组下的所有键值对
            /// </summary>
            /// <param name="filePtah">文件路径</param>
            /// <param name="groupName">分组名称</param>
            /// <returns></returns>
            public static List<ValueTuple<string, string>> GetGroupValues(string filePath, string groupName)
            {
                List<ValueTuple<string, string>> resultList = new List<ValueTuple<string, string>>();
                filePath = GetAsbPath(filePath);
                byte[] bts = new byte[1024 * 2];
                int readNu = GetPrivateProfileSection(groupName, bts, bts.Length, filePath);
                string readStr = Encoding.Default.GetString(bts, 0, readNu);
                string[] readStrSplicArr = readStr.Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries);

                string[] valArr = null;
                foreach (string itemStr in readStrSplicArr)
                {
                    if (itemStr.Contains("="))
                    {
                        valArr = itemStr.Split('=');
                        if (valArr.Length >= 1)
                        {
                            resultList.Add((valArr[0], valArr[1]));
                        }
                    }
                }
                return resultList;
            }

            private static string GetAsbPath(string path)
            {
                if (!Path.IsPathRooted(path))
                {
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
                }
                return path;
            }

            public static bool SetValue(string sectionName, string key, string value, string filePath)
            {
                bool result;
                filePath = GetAsbPath(filePath);
                try
                {
                    result = ((int)WritePrivateProfileString(sectionName, key, value, filePath) > 0);
                }
                catch
                {
                    return false;
                }
                return result;
            }

            public static bool RemoveSection(string sectionName, string filePath)
            {
                bool result;
                filePath = GetAsbPath(filePath);
                try
                {
                    result = ((int)WritePrivateProfileString(sectionName, null, "", filePath) > 0);
                }
                catch
                {
                    return false;
                }
                return result;
            }

            public static bool Removekey(string sectionName, string key, string filePath)
            {
                bool result;
                filePath = GetAsbPath(filePath);
                try
                {
                    result = ((int)WritePrivateProfileString(sectionName, key, null, filePath) > 0);
                }
                catch
                {
                    return false;
                }
                return result;
            }
        }

调用系统C++代码能够很大程度上减少自己去维护 数据结构,应为引用了ValueTuple 要求net框架至少在 4.7 以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值