示例#1
0
        /// <summary>
        /// 读取数据列可见性
        /// </summary>
        /// <param name="cfgFile">配置文件</param>
        /// <param name="dgvName">数据表名</param>
        /// <returns>数据列可见性</returns>
        public static Dictionary <string, bool> LoadColumnVisibility(string cfgFile, string dgvName)
        {
            //读取配置
            string strStatu = IniOperate.CF_ReadConfig(cfgFile, "DataGridView", dgvName).Trim();

            //转换为状态字典
            Dictionary <string, bool> result = new Dictionary <string, bool>();

            try
            {
                string[] columns = strStatu.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string column in columns)
                {
                    result.Add(
                        column.Split('|')[0].Trim(),
                        column.Split('|')[1].ToUpper().Trim() == "TRUE"
                        );
                }
            }
            catch
            {
                result.Clear();
            }

            return(result);
        }
        /// <summary>
        /// INI文件操作测试
        /// </summary>
        private void IniFileOperateTest()
        {
            string iniPath = Path.Combine(Environment.CurrentDirectory, "TestConfig.ini");

            PrintLogLn(MsgType.Info, $"INI文件路径: {iniPath}");

            PrintLogLn(MsgType.Warn, "静态方法测试!");

            string value0 = GetRandomString(10);

            PrintLogLn(MsgType.Info, $"生成随机字符串: {value0}");

            IniOperate.CF_WriteConfig(iniPath, "TestSection", "StringKey", value0.ToString());
            PrintLogLn(MsgType.Info, $"写入配置: TestSection->StringKey -> {value0}");

            string result0 = IniOperate.CF_ReadConfig(iniPath, "TestSection", "StringKey");

            PrintLogLn(MsgType.Info, $"读取配置: TestSection->StringKey -> {result0}");

            if (value0 == result0)
            {
                PrintLogLn(MsgType.Success, $"写入值与读取值比对成功!");
            }
            else
            {
                PrintLogLn(MsgType.Error, $"写入值与读取值比对失败!");
            }

            double value1 = Math.Round(new Random().NextDouble(), 10);

            PrintLogLn(MsgType.Info, $"生成随机小数: {value1}");

            IniOperate.CF_WriteConfig <double>(iniPath, "TestSection", "DoubleKey", value1);
            PrintLogLn(MsgType.Info, $"写入配置: TestSection->DoubleKey -> {value1}");

            double result1 = IniOperate.CF_ReadConfig <double>(iniPath, "TestSection", "DoubleKey");

            PrintLogLn(MsgType.Info, $"读取配置: TestSection->DoubleKey -> {result1}");

            if (value1 == result1)
            {
                PrintLogLn(MsgType.Success, $"写入值与读取值比对成功!");
            }
            else
            {
                PrintLogLn(MsgType.Error, $"写入值与读取值比对失败!");
            }

            ETest value2 = (ETest) new Random().Next(1, 6);

            PrintLogLn(MsgType.Info, $"生成随机枚举值: {value2}");

            IniOperate.CF_WriteConfig <ETest>(iniPath, "TestSection", "EnumKey", value2);
            PrintLogLn(MsgType.Info, $"写入配置: TestSection->EnumKey -> {value2}");

            ETest result2 = IniOperate.CF_ReadConfig <ETest>(iniPath, "TestSection", "EnumKey");

            PrintLogLn(MsgType.Info, $"读取配置: TestSection->EnumKey -> {result2}");

            if (value2 == result2)
            {
                PrintLogLn(MsgType.Success, $"写入值与读取值比对成功!");
            }
            else
            {
                PrintLogLn(MsgType.Error, $"写入值与读取值比对失败!");
            }

            PrintLogLn(MsgType.Warn, "动态方法测试!");

            IniOperate iniOperate = new IniOperate(iniPath);

            value0 = GetRandomString(10);
            PrintLogLn(MsgType.Info, $"生成随机字符串: {value0}");

            iniOperate.CF_WriteConfig("TestSection", "StringKey", value0.ToString());
            PrintLogLn(MsgType.Info, $"写入配置: TestSection->StringKey -> {value0}");

            result0 = iniOperate.CF_ReadConfig("TestSection", "StringKey");
            PrintLogLn(MsgType.Info, $"读取配置: TestSection->StringKey -> {result0}");

            if (value0 == result0)
            {
                PrintLogLn(MsgType.Success, $"写入值与读取值比对成功!");
            }
            else
            {
                PrintLogLn(MsgType.Error, $"写入值与读取值比对失败!");
            }

            value1 = Math.Round(new Random().NextDouble(), 10);
            PrintLogLn(MsgType.Info, $"生成随机小数: {value1}");

            iniOperate.CF_WriteConfig("TestSection", "DoubleKey", value1);
            PrintLogLn(MsgType.Info, $"写入配置: TestSection->DoubleKey -> {value1}");

            result1 = iniOperate.CF_ReadConfig <double>("TestSection", "DoubleKey");
            PrintLogLn(MsgType.Info, $"读取配置: TestSection->DoubleKey -> {result1}");

            if (value1 == result1)
            {
                PrintLogLn(MsgType.Success, $"写入值与读取值比对成功!");
            }
            else
            {
                PrintLogLn(MsgType.Error, $"写入值与读取值比对失败!");
            }

            value2 = (ETest) new Random().Next(1, 6);
            PrintLogLn(MsgType.Info, $"生成随机枚举值: {value2}");

            iniOperate.CF_WriteConfig("TestSection", "EnumKey", value2);
            PrintLogLn(MsgType.Info, $"写入配置: TestSection->EnumKey -> {value2}");

            result2 = iniOperate.CF_ReadConfig <ETest>("TestSection", "EnumKey");
            PrintLogLn(MsgType.Info, $"读取配置: TestSection->EnumKey -> {result2}");

            if (value2 == result2)
            {
                PrintLogLn(MsgType.Success, $"写入值与读取值比对成功!");
            }
            else
            {
                PrintLogLn(MsgType.Error, $"写入值与读取值比对失败!");
            }

            List <string> sections = iniOperate.CF_ReadSections();

            PrintLogLn(MsgType.Info, $"读取节点个数:{sections.Count}");
            PrintLog(MsgType.Info, $"读取节点:");
            foreach (string section in sections)
            {
                PrintMsg(MsgType.Info, $"{section};");
            }
            PrintMsgLn(MsgType.Info, "");

            List <string> keys = iniOperate.CF_ReadKeys("TestSection");

            PrintLogLn(MsgType.Info, $"读取键名个数:{keys.Count}");
            PrintLog(MsgType.Info, $"读取键名:");
            foreach (string key in keys)
            {
                PrintMsg(MsgType.Info, $"{key};");
            }
            PrintMsgLn(MsgType.Info, "");

            File.Delete(iniPath);
        }