I found solution and rectified the problem. Thanks to jochen and OriginalGriff.
below I am pasting the complete final code. Maybe it will useful for some one.
I found in net a solution that they told to add true while opensubkey. I added and that solved my problem
Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings");
if (key != null)
{
key.Close();
Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other");
if (subkey != null)
{
subkey.Close();
uncheck();
}
else
{
checkService();
getipl();
crelogin();
chklogin();
chkpass();
}
}
else
{
RegistryKey subkey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\OurSettings\other");
MessageBox.Show("Created");
subkey.SetValue("InstallDate", DateTime.Now.ToBinary(), RegistryValueKind.QWord);
subkey.SetValue("number", cn, RegistryValueKind.QWord);
subkey.Close();
uncheck();
}
checkService();
getipl();
crelogin();
chklogin();
chkpass();
}
public void uncheck()
{
Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other",true);
try
{
var stat = (long)subkey.GetValue("number", "st");
var result = (long)subkey.GetValue("InstallDate", "date");
var date = DateTime.FromBinary(result);
MessageBox.Show("Install Date" + " " + date.ToString());
DateTime comp = date.AddDays(1);
var dt = DateTime.Now;
MessageBox.Show("next Lock Date" + " " + comp.ToString());
if (date >= comp)
{
con.Close();
con.Open();
SqlCommand cmd = new SqlCommand("delete comapny where ctype='b'", con);
cmd.ExecuteNonQuery();
con.Close();
cn = 1;
subkey.SetValue("number", cn, RegistryValueKind.QWord);
subkey.Close();
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Close();
}
else if (dt >= comp)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete comapny where ctype='b'", con);
cmd.ExecuteNonQuery();
con.Close();
cn = 1;
subkey.SetValue("number", cn, RegistryValueKind.QWord);
subkey.Close();
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Close();
}
else if (stat!= 0)
{
con.Open();
SqlCommand cmd = new SqlCommand("delete comapny where ctype='b'", con);
cmd.ExecuteNonQuery();
con.Close();
subkey.Close();
}
else
{
checkService();
getipl();
crelogin();
chklogin();
chkpass();
}
}
catch (Exception ex)
{
MessageBox.Show("Software Is Locked. Contact 9290345689");
this.Close();
}
}