Skip to content
QQ edited this page Jan 28, 2022 · 12 revisions

Welcome to the QSoft.Ini wiki!

hope this document can help your work.

Simple example

Setting setting = new Setting()
{
    IP = "127.0.0.1",
    Port = 88
};
string ini_str = IniConvert.SerializeObject(setting);
/*
[Setting]
IP=127.0.0.1
Port=88
*/
var deserialize = IniConvert.DeserializeObject<Setting>(ini_str);

Attribute

IniIgnore

Use attribute ignore exist property

public class Setting
{
    [IniIgnore]
    public string IP { set; get; }
    public int Port { set; get; }
}
string ini_str = IniConvert.SerializeObject(setting);
/*
[Setting]
Port=88
*/

IniSectionKey

Use attribute ignore exist property

public class Setting
{
        [IniSectionKey()]
        public string IP { set; get; }
        [IniSectionKey(Key ="DefaultPort")]
        public int Port { set; get; }
}
string ini_str = IniConvert.SerializeObject(setting);
/*
[Setting]
IP=127.0.0.1
DefaultPort=88
*/

IniSection

Use attribute ignore exist property

[IniSection(Name = "Address")]
public class Setting
{
    public string IP { set; get; }
    public int Port { set; get; }
}  

Setting setting = new Setting()
{
    IP = "127.0.0.1",
    Port = 88
};  

string ini_str = IniConvert.SerializeObject(setting);
/*
[Address]
IP=127.0.0.1
Port=88
*/

if property is object which like class, default transform xml

[IniSection(Name = "Address")]
public class RemoteSetting
{
    public string IP { set; get; }
    public int Port { set; get; }
    public string Account { set; get; }
    public string Password { set; get; }
}

public class Setting
{
    public string IP { set; get; }
    public int Port { set; get; }
    public RemoteSetting Ftp { set; get; }
}  

Setting setting = new Setting()
{
    IP = "127.0.0.1",
    Port = 88,
    Ftp_Log = new RemoteSetting() { IP = "127.0.0.100", Port = 50, Account = "Allen", Password = "123" }
};  

string ini_str = IniConvert.SerializeObject(setting);
/*
[Address]
IP=127.0.0.1
Port=88
Ftp_Log=<?xml version="1.0" encoding="utf-8"?><RemoteSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><IP>127.0.0.100</IP><Port>50</Port><Account>Allen</Account><Password>123</Password></RemoteSetting>
*/

but add IniSection attribute to the property

[IniSection(Name = "Address")]
public class Setting
{
    public string IP { set; get; }
    public int Port { set; get; }
    [IniSection(Name = "Ftp1")]
    public RemoteSetting Ftp_Log { set; get; }
}

Setting setting = new Setting()
{
    IP = "127.0.0.1",
    Port = 88,
    Ftp_Log = new RemoteSetting() { IP = "127.0.0.100", Port = 50, Account = "Allen", Password = "123" }
}; 

string ini_str = IniConvert.SerializeObject(setting);

/*
[Address]
IP=127.0.0.1
Port=88

[Ftp1]
IP=127.0.0.100
Port=50
Account=Allen
Password=123
*/

Clone this wiki locally