page contents

C#操作ini文件的帮助类

本文讲述了C#操作ini文件的帮助类!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-08-RPiWgOuk63097ebb13750.png本文讲述了C#操作ini文件的帮助类!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理界面就可实现相同的配置了。但在某些情况,还是要直接编辑ini才方便,一般只有很熟悉windows才能去直接编辑。

一、定义Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Runtime.InteropServices;
using System.Text;
 
namespace IniDemo
{
    public class IniFile
    {
        private string m_FileName;
 
        public string FileName
        {
            get
            {
                return this.m_FileName;
            }
            set
            {
                this.m_FileName = value;
            }
        }
 
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
 
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
 
        [DllImport("kernel32.dll")]
        private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
 
        public IniFile(string aFileName)
        {
            this.m_FileName = aFileName;
        }
 
        public IniFile()
        {
        }
 
        public int ReadInt(string section, string name, int def)
        {
            return IniFile.GetPrivateProfileInt(section, name, def, this.m_FileName);
        }
 
        public string ReadString(string section, string name, string def)
        {
            StringBuilder stringBuilder = new StringBuilder(2048);
            IniFile.GetPrivateProfileString(section, name, def, stringBuilder, 2048, this.m_FileName);
            return stringBuilder.ToString();
        }
 
        public void WriteInt(string section, string name, int Ival)
        {
            IniFile.WritePrivateProfileString(section, name, Ival.ToString(), this.m_FileName);
        }
 
        public void WriteString(string section, string name, string strVal)
        {
            IniFile.WritePrivateProfileString(section, name, strVal, this.m_FileName);
        }
 
        public void DeleteSection(string section)
        {
            IniFile.WritePrivateProfileString(section, null, null, this.m_FileName);
        }
 
        public void DeleteAllSection()
        {
            IniFile.WritePrivateProfileString(null, null, null, this.m_FileName);
        }
 
        public string IniReadValue(string section, string name)
        {
            StringBuilder stringBuilder = new StringBuilder(256);
            IniFile.GetPrivateProfileString(section, name, "", stringBuilder, 256, this.m_FileName);
            return stringBuilder.ToString();
        }
 
        public void IniWriteValue(string section, string name, string value)
        {
            IniFile.WritePrivateProfileString(section, name, value, this.m_FileName);
        }
    }
}

二、调用方法

1
2
3
4
5
6
7
8
9
IniFile iniFile = new IniFile(Environment.CurrentDirectory + "\\LocalInf.ini");
//读取Local节点下M的值,默认为空值
string m  = iniFile.ReadString("Local", "M", "");
//Local节点下写F=f
iniFile.WriteString("Local", "F", "f");
//读取Local节点下IsSleep的字符串值,并转为bool类型值,给出默认值为False
 bool f = bool.Parse(iniFile.ReadString("Local", "IsSleep", "False"));
//读取Local节点下的C的字符串值,并转为double类型值,给出默认值0
 bool f = double.Parse(iniFile.ReadString("Local", "C", "0"));

到此这篇关于C#操作ini文件帮助类的文章就介绍到这了。更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-08-27 10:17
  • 阅读 ( 400 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1478 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章