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
*/

IniArray

if property is object like list, default transform xml

public class Setting
{
    public string IP { set; get; }
    public int Port { set; get; }
    public List<TestItem> TestItems1 { set; get; }
}

string ini_str = IniConvert.SerializeObject(setting);

/*
[Setting]
IP=127.0.0.1
Port=88
TestItems1=<?xml version="1.0" encoding="utf-8"?><ArrayOfTestItem xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><TestItem><Exe>A.exe</Exe><Command>qq</Command></TestItem><TestItem><Exe>B.bat</Exe><Command /></TestItem><TestItem><Exe>taskmgr.exe</Exe></TestItem></ArrayOfTestItem>
*/

if you add IniArray attribute to this property

public class Setting
{
    public string IP { set; get; }
    public int Port { set; get; }
    [IniArray(Name ="Test")]
    public List<TestItem> TestItems1 { set; get; }
}

Setting setting = new Setting()
{
    IP = "127.0.0.1",
    Port = 88,
    TestItems1 = new List<TestItem>()
    {
        new TestItem(){Exe = "A.exe", Command="qq"},
        new TestItem(){Exe="B.bat", Command=""},
        new TestItem(){Exe="taskmgr.exe"}
    }
};

string ini_str = IniConvert.SerializeObject(setting);

/*
[Setting]
IP=127.0.0.1
Port=88

[Test_0]
Exe=A.exe
Command=qq

[Test_1]
Exe=B.bat
Command=

[Test_2]
Exe=taskmgr.exe

*/
Clone this wiki locally