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

LVL 1
peterkiersAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
In that case there is nothing to clean up. You IniFile class appears to just be calling into API methods. You only need to have "clean up" code in you class if you are creating and using objects that use unmanaged resources, which your IniFile class doesn't.
0
 
peterkiersAuthor Commented:
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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
What is IniFile? Is it a custom class of your own?
0
 
peterkiersAuthor Commented:
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

0
 
peterkiersAuthor Commented:
Thanks Carl I didn't knew that.

Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.