Link to home
Create AccountLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Clean up resource of an inifile

Hi,

I have a main form called MainForm and a second form called Settings.
The settingsform is there for the users to adjust the components-settings
On the MainForm. I have this code in the settings-form to load and save the
settings of the componens to a inifile.

But after the settings-form is close by the user doesn't the resource inifile
has to be cleaned up.

I have this declared in the beginning of the settingsform:

namespace Knowledgebase
{
    public partial class Settings : Form
    {
        IniFile _if;

        public Settings()
        {
            InitializeComponent();
            _if = new IniFile(System.IO.Path.Combine(Application.StartupPath, "DiaManager.ini"));
        }

Who knows the answer and is willing to help me?

Peter
/*--------------------------------------------------------------------*/
        private void Settings_Load(object sender, EventArgs e)
        {
            chkButtonFont.Checked = Boolean.Parse(_if.IniReadValue("NAVBAR", "chkButtonFont", ""));
            chkRemoveBtn.Checked = Boolean.Parse(_if.IniReadValue("NAVBAR", "chkRemoveBtn", ""));
            chkSmallBtn.Checked = Boolean.Parse(_if.IniReadValue("NAVBAR", "chkSmallBtn", ""));
            chkShowMin.Checked = Boolean.Parse(_if.IniReadValue("AGENDA", "chkShowMin", ""));
           // txtbInput.Text = _if.IniReadValue("NOTIFY", "txtbInput", "");
        }
        /*--------------------------------------------------------------------*/
        private void Settings_FormClosed(object sender, FormClosedEventArgs e)
        {
            _if.IniWriteValue("NAVBAR", "chkButtonFont", chkButtonFont.Checked.ToString());
            _if.IniWriteValue("NAVBAR", "chkRemoveBtn", chkRemoveBtn.Checked.ToString());
            _if.IniWriteValue("NAVBAR", "chkSmallBtn", chkSmallBtn.Checked.ToString());
            _if.IniWriteValue("AGENDA", "chkShowMin", chkShowMin.Checked.ToString());
          //  _if.IniWriteValue("NOTIFY", "txtbInput", txtbInput.Text);
        }
        /*--------------------------------------------------------------------*/

Open in new window

Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

I have tried it myself bu I get an error:

        private void Settings_Load(object sender, EventArgs e)
        {
            //if (File.Exists(INIPath))
            {
                try
                {
                 chkButtonFont.Checked = Boolean.Parse(_if.IniReadValue("NAVBAR", "chkButtonFont", ""));
                 chkRemoveBtn.Checked = Boolean.Parse(_if.IniReadValue("NAVBAR", "chkRemoveBtn", ""));
                 chkSmallBtn.Checked = Boolean.Parse(_if.IniReadValue("NAVBAR", "chkSmallBtn", ""));
                 chkShowMin.Checked = Boolean.Parse(_if.IniReadValue("AGENDA", "chkShowMin", ""));
                 // txtbInput.Text = _if.IniReadValue("NOTIFY", "txtbInput", "");
                    }
                catch (Exception ex)
                {
             throw ex;
           }
           finally
            {
                if (_if != null)
                    _if.Close();  <--------------------------------
            }
           
        }
        }

'Knowledgebase.Settings.IniFile' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'Knowledgebase.Settings.IniFile' could be found (are you missing a using directive or an assembly reference?)      

Who can help me?
Avatar of Carl Tawn
What is IniFile? Is it a custom class of your own?
Hey Carl,

I have put the whole class in the code-section.

P.
public partial class Settings : Form
    {
        IniFile _if;

        public Settings()
        {
            InitializeComponent();
            _if = new IniFile(System.IO.Path.Combine(Application.StartupPath, "DiaManager.ini"));
        }
        /*--------------------------------------------------------------------*/
        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);

            public IniFile(string INIPath)
            { path = INIPath; }

            public void IniWriteValue(string Section, string Key, string Value)
            { WritePrivateProfileString(Section, Key, Value, this.path); }

            public string IniReadValue(string Section, string Key, string Default)
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 1024, this.path);
                return (temp.Length > 0) ? temp.ToString() : Default;
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks Carl I didn't knew that.

Peter