private void HostsLockCheckbox_CheckedChanged(object sender, EventArgs e)
{
// Save config
Config.LockHostsFile = HostsLockCheckbox.Checked;
Config.Save();
// We only do something if redirects are enabled
if (Redirector.RedirectsEnabled)
{
// Grab our hosts file
SysHostsFile hFile = Redirector.HostsFileSys;
// Lock or unlock the file
if (HostsLockCheckbox.Checked && !SysHostsFile.IsLocked)
{
// Attempt to lock the hosts file
if (!hFile.Lock())
{
MessageBox.Show(
"Unable to lock the HOSTS file! Reason: " + hFile.LastException.Message,
"Hosts File Error", MessageBoxButtons.OK, MessageBoxIcon.Error
);
return;
}
// Update the GUI
HostsLockStatus.Text = "Locked";
HostsLockStatus.ForeColor = Color.Green;
HostsStatusPic.Image = Resources.check;
Tipsy.SetToolTip(HostsStatusPic, "Gamespy redirects are currently active.");
}
else if (!HostsLockCheckbox.Checked && SysHostsFile.IsLocked)
{
// Attempt to unlock the hosts file
if (!hFile.UnLock())
{
MessageBox.Show(
"Unable to unlock the HOSTS file! Reason: " + hFile.LastException.Message,
"Hosts File Error", MessageBoxButtons.OK, MessageBoxIcon.Error
);
return;
}
// Update the GUI
HostsLockStatus.Text = "UnLocked";
HostsLockStatus.ForeColor = Color.Red;
HostsStatusPic.Image = Resources.warning;
Tipsy.SetToolTip(HostsStatusPic, "HOSTS file is unlocked, Redirects will not work!");
}
}
}