Help with code

09/02/2017 11:07 anno116611#1
Hi,

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)
and "data" for reading, writing key.
Code:
var parser = new FileIniDataParser();
parser.Parser.Configuration.AllowDuplicateKeys = true;
IniData data = parser.ReadFile(myFilePath);
Here is class link : [Only registered and activated users can see links. Click Here To Register...]

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


    }
}
09/02/2017 12:47 Serraniel#2
For a better structure in your code design your should encapsulate your search function, in this case with a parameter.
Also you are only searching one index inside the section than all keys. This may cause exceptions if there only is one item as you always try to access the second.

Your code may look like this then:
PHP Code:
        private List<stringSearchItems(string iniFilestring searchFor)
        {
            var 
results = new List<string>();

            var 
parser = new FileIniDataParser();
            var 
data parser.ReadFile(iniFile);

            foreach (var 
section in data.Sections)
            {
                foreach (var 
key in section.Keys)
                {
                    if(
string.Equals(key.KeyNamesearchForStringComparison.CurrentCultureIgnoreCase))
                        
results.Add($"[{section}] {key.KeyName}");
                }
            }

            return 
results;
        } 
If you are up with LINQ you can use this instead of the stacked foreach:
PHP Code:
        private List<stringSearchItems(string iniFilestring searchFor)
        {
            var 
parser = new FileIniDataParser();
            var 
data parser.ReadFile(iniFile);

            return (
from section in data.Sections from key in section.Keys where string.Equals(key.KeyNamesearchForStringComparison.CurrentCultureIgnoreCaseselect $"[{section}] {key.KeyName}").ToList();
        } 
09/02/2017 15:39 anno116611#3
Quote:
Originally Posted by Serraniel View Post
For a better structure in your code design your should encapsulate your search function, in this case with a parameter.
Also you are only searching one index inside the section than all keys. This may cause exceptions if there only is one item as you always try to access the second.

Your code may look like this then:
PHP Code:
        private List<stringSearchItems(string iniFilestring searchFor)
        {
            var 
results = new List<string>();

            var 
parser = new FileIniDataParser();
            var 
data parser.ReadFile(iniFile);

            foreach (var 
section in data.Sections)
            {
                foreach (var 
key in section.Keys)
                {
                    if(
string.Equals(key.KeyNamesearchForStringComparison.CurrentCultureIgnoreCase))
                        
results.Add($"[{section}] {key.KeyName}");
                }
            }

            return 
results;
        } 
Can you show me a example how to use this since im beginner in c# and can't make this to give me any results.

I need to:
-open file ( works )
-put file path in variable ( works )
-open ini for edit and hold it opened ( don't know how to make this )
-when i search in ini to show me all result where name from input box matching keys in ini ( works )
-put founded items in list ( works )
-when i click on item set id and name to boxes ( works )

Only trouble i have with this because every time i press search button it reads file and its slow ! So i need file to be opened all time and this to store somewhere else so when i press search button to call only function not to read file on every search.
Code:
var parser = new FileIniDataParser();
parser.Parser.Configuration.AllowDuplicateKeys = true;
IniData data = parser.ReadFile(myFilePath);
09/02/2017 19:55 .QaDusch#4
Quote:
Originally Posted by anno116611 View Post

So i need file to be opened all time and this to store somewhere else so when i press search button to call only function not to read file on every search.
Couldnt you just use an Array/List for that?
Store the setting there and let a function search trough the items.

That would take more of your memory, but that should be fine if you dont use it as a "commercial" project.
09/03/2017 14:14 anno116611#5
Quote:
Originally Posted by .QaDusch View Post
Couldnt you just use an Array/List for that?
Store the setting there and let a function search trough the items.

That would take more of your memory, but that should be fine if you dont use it as a "commercial" project.
What's the point of array if this will be slow in end ? I need to find a file when i press search without waiting 1-2 seconds.
09/03/2017 14:19 .QaDusch#6
Quote:
Originally Posted by anno116611 View Post
What's the point of array if this will be slow in end ? I need to find a file when i press search without waiting 1-2 seconds.
I guess I misunderstood your problem.
Searching trough an Array should be faster then searching trough an existing .txt/.ini file.

Correct me if iam wrong.