I want to make it belong to the value with section at the top.But now the .ini file is not being created.

  • Thread starter Thread starter chh.552
  • Start date Start date
C

chh.552

Guest
image





See url above.If you don't put a section as in the grid image above, I want to make it belong to the value with section at the top.But now the ini file is not being created.If the session value is empty, I want the session value to follow the highest session value entered.


my code

using System;
using System.Data;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;

namespace WindowsForms
{
public partial class Test : Form
{
public Test()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

DataTable table = new DataTable();
table.Columns.Add("Section", typeof(string));
table.Columns.Add("Key", typeof(string));
table.Columns.Add("Value", typeof(string));
table.Rows.Add("[App_Info]", "1", "2");
table.Rows.Add("", "3", "4");
table.Rows.Add("", "5", "6");
table.Rows.Add("", "7", "8");
table.Rows.Add("", "9", "10");
table.Rows.Add("", "11", "12");
table.Rows.Add("", "13", "14");
table.Rows.Add("", "15", "16");
table.Rows.Add("[LOG_INFO]", "17", "18");
table.Rows.Add("", "19", "20");
table.Rows.Add("", "21", "22");
table.Rows.Add("", "23", "24");
table.Rows.Add("", "25", "26");
table.Rows.Add("", "27", "28");
table.Rows.Add("[MAIN_CONTROL]", "29", "30");
table.Rows.Add("", "31", "32");
table.Rows.Add("", "33", "34");
table.Rows.Add("", "35", "36");
table.Rows.Add("", "37", "38");
table.Rows.Add("", "39", "40");

dataGridView1.DataSource = table;
dataGridView1.AllowUserToAddRows = false;

}

private void button2_Click(object sender, EventArgs e)
{
IniFile iniFile = new IniFile(@"1-iniFile.ini");

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows.Cells[0].Value.ToString()=="")
{
iniFile.IniWriteValue("App_Info", dataGridView1.Rows.Cells[1].Value.ToString(), dataGridView1.Rows.Cells[2].Value.ToString());
}
else
{
iniFile.IniWriteValue(dataGridView1.Rows.Cells[0].Value.ToString(), dataGridView1.Rows.Cells[1].Value.ToString(), dataGridView1.Rows.Cells[2].Value.ToString());
}

}
MessageBox.Show("successfully");

}
}
/// <summary>
/// Create a New INI file to store or load data
/// </summary>
public class IniFile
{
public string path;

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);

/// <summary>
/// INIFile Constructor.
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string INIPath)
{
path = INIPath;
}
/// <summary>
/// Write Data to the INI File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="Value"></PARAM>
/// Value Name
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}

/// <summary>
/// Read Data Value From the Ini File
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, this.path);
return temp.ToString();

}
}

}



create .ini

App_Info]
1=2
3=4
5=6
7=8
9=10
11=12
27=28
..
..
..
..
[MAIN_CONTROL]]
40=41
[main_device]
42=43
Come out like this. Originally

[App_Info]
1=2
3=4
5=6
7=8
[sub_info]
9=10
11=12
27=28
..
..
..
..
[MAIN_CONTROL]]
40=41
...
...
[main_device]
50=51
....

Continue reading...
 
Back
Top