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
Peter KiersOperatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peter KiersOperatorAuthor 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
Peter KiersOperatorAuthor 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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KiersOperatorAuthor Commented:
Thanks Carl I didn't knew that.

Peter
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.