Peter Kiers
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.Com bine(Appli cation.Sta rtupPath, "DiaManager.ini"));
}
Who knows the answer and is willing to help me?
Peter
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.Com
}
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);
}
/*--------------------------------------------------------------------*/
What is IniFile? Is it a custom class of your own?
ASKER
Hey Carl,
I have put the whole class in the code-section.
P.
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;
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks Carl I didn't knew that.
Peter
Peter
ASKER
private void Settings_Load(object sender, EventArgs e)
{
//if (File.Exists(INIPath))
{
try
{
chkButtonFont.Checked = Boolean.Parse(_if.IniReadV
chkRemoveBtn.Checked = Boolean.Parse(_if.IniReadV
chkSmallBtn.Checked = Boolean.Parse(_if.IniReadV
chkShowMin.Checked = Boolean.Parse(_if.IniReadV
// txtbInput.Text = _if.IniReadValue("NOTIFY",
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (_if != null)
_if.Close(); <-------------------------
}
}
}
'Knowledgebase.Settings.In
Who can help me?