I have a class for reading a ini files.
Problem i have with it is that i can't use it where i want.
For my example code i need to define class and use only part of it to read/search in ini file.
So when i try to search for string ( key ) it read file every time so it's slow as hell. I need to read file, put in variable so i can use it in search function.
If i use this code outside of search function then it won't read ini, and i need this function when i open a file to stay in memory until i exit a program, so i can use
Code:
foreach (var item in data.Sections)
Code:
var parser = new FileIniDataParser(); parser.Parser.Configuration.AllowDuplicateKeys = true; IniData data = parser.ReadFile(myFilePath);

Here is code :
Code:
using System;
using System.Windows.Forms;
using IniParser;
using IniParser.Model;
using System.Collections.Generic;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public string myFilePath;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
searchBtn.Enabled = false;
writeIniBtn.Enabled = false;
saveBtn.Enabled = false;
}
private void openIniBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = "c:\\";
openFile.Filter = "INI files (*.ini)|*.ini";
openFile.Title = "Select a INI file for edit";
if (openFile.ShowDialog() == DialogResult.OK)
{
myFilePath = openFile.FileName; // full path to a file
// enable buttons
searchBtn.Enabled = true;
writeIniBtn.Enabled = true;
saveBtn.Enabled = true;
openIniBtn.Enabled = false; // disable open ini button
}
}
private void searchBtn_Click(object sender, EventArgs e)
{
// this part read ini file and search inside ini
var parser = new FileIniDataParser();
parser.Parser.Configuration.AllowDuplicateKeys = true;
IniData data = parser.ReadFile(myFilePath); // opens ini and search in it , myFilePath = path to ini file
////////////////////////////////////////////////
listView1.Items.Clear();
var searchItem = searchInput.Text; // getting data from input to search for
foreach (var item in data.Sections)
{
if (item.Keys["1"].IndexOf(searchItem, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
// add founded items to list
var foundedItems = ($"[{item.SectionName}] { item.Keys["1"] } ");
ListViewItem listviewitem = new ListViewItem(foundedItems);
listView1.Items.Add(listviewitem);
}
}
}
}
}







