I want the section not to be duplicated.

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

chh.552

Guest
My code.

When the .ini file is generated, [app_info] appears repeatedly.
I would like to be able to create only the part of the session where I entered the section value like the image above.


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();

}
}

}
MY INI 파일
[[App_Info]]
1=2
[[App_Info]]
3=4
[[App_Info]]
5=6
[[App_Info]]
7=8
[[App_Info]]
9=10
[[App_Info]]
11=12
[[App_Info]]
13=14
[[App_Info]]
15=16
[[LOG_INFO]]
17=18
[[App_Info]]
19=20
[[App_Info]]
21=22
[[App_Info]]
23=24
[[App_Info]]
25=26
[[App_Info]]
27=28
[[MAIN_CONTROL]]
29=30
[[App_Info]]
31=32
[[App_Info]]
33=34
[[App_Info]]
35=36
[[App_Info]]
37=38
[[App_Info]]
39=40

Continue reading...
 
Back
Top