Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 08:18

  • Please register to post and access all features, it's quick, easy and FREE!


Help with code

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Aug 2017
Posts: 11
Received Thanks: 0
Question Help with code

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 :

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


    }
}



anno116611 is offline  
Old 09/02/2017, 12:47   #2
SweetnessOverflow セラニエル



 
elite*gold: 1000
The Black Market: 202/1/0
Join Date: May 2010
Posts: 6,637
Received Thanks: 4,956
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();
        } 


Serraniel is offline  
Old 09/02/2017, 15:39   #3
 
elite*gold: 0
Join Date: Aug 2017
Posts: 11
Received Thanks: 0
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);
anno116611 is offline  
Old 09/02/2017, 19:55   #4
 
elite*gold: 1000
The Black Market: 413/0/0
Join Date: Aug 2009
Posts: 8,856
Received Thanks: 1,096
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.


.QaDusch is offline  
Old 09/03/2017, 14:14   #5
 
elite*gold: 0
Join Date: Aug 2017
Posts: 11
Received Thanks: 0
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.
anno116611 is offline  
Old 09/03/2017, 14:19   #6
 
elite*gold: 1000
The Black Market: 413/0/0
Join Date: Aug 2009
Posts: 8,856
Received Thanks: 1,096
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.


.QaDusch is offline  
Reply

Tags
c#, ini files, reading ini



« Setting file path to variable | How to monitor changes on screen »

Similar Threads
Waffen Code´s - Premium Code´s - Dinar Code´s [VERKAUFBAR] ! ! !
03/10/2011 - WarRock Trading - 3 Replies
Hallo Leute , ich verkaufe Code´s . Die Code´s werde ich jetzt darunter hinschreiben und ihr könnt euch bei mir melden 50 % Günstiger Als WarRock Marketplace . 1.Waffen Code´s >>> G36 Code Xm8 Code Javelin Code Mp7a1 Code Famas Retail...
Waffen Code´s - Premium Code´s - Dinar Code´s [VERKAUFBAR] ! ! !
03/10/2011 - WarRock Trading - 9 Replies
Waffen Code´s - Premium Code´s - Dinar Code´s ! ! ! Hallo Leute , ich verkaufe Code´s . Die Code´s werde ich jetzt darunter hinschreiben und ihr könnt euch bei mir melden 50 % Günstiger Als WarRock Marketplace . 1.Waffen Code´s >>> G36...
°°xm8 code°°katana code°° Javelin code°°
12/21/2010 - WarRock Trading - 0 Replies
Habe Codes Bei Intresse bitte in Skype adden: war.rock.16 Ich will dafür nur psc reste ;D:D
B> DriftCity CBS Code | S> War Rock Code / Bounty Bay Code etc.
10/05/2010 - Trading - 1 Replies
Hi, wie schon im Titel beschrieben. In der CBS vom November gab es Bonusodes für mehrere Spiele. Ich benötige DriftCity Codes. Kann sonst für alle anderen Spiele die Codes biten, einige auch doppelt. Hier eine Liste der Spiele und Bonusaktionen:...
help help help help help help
06/28/2009 - Say Hello - 0 Replies
how i can dowmload Mangos 6385 ??????????????????????????????????? please give me the limk i can't see that i know it is in www.elitepvpers.com/.../153716-release-mangos-releases-blackscorpian-win32-2-4-3-a.html - but give me link...



All times are GMT +2. The time now is 08:18.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Abuse
Copyright ©2018 elitepvpers All Rights Reserved.