Блокировка моего программного обеспечения после указанного времени в C#
Я написал некоторый код для блокировки моего программного обеспечения после указанного времени. Для этого я использовал реестр.
I created One sub-key and checking there whether my software is running first time or not. If first time then I am creating sub-key and storing installation date.I am reading that date and adding some days and checking every time when user opened my software . Here Everything is working fine but after locked the software if user changes date and restore data then I am unable to track that. So I added other sub-key for that and when ever i am locking software I am replacing that sub-key value with 1.using that I want to check already one time locked software so if he restored data and changed date also I can lock the software. Here only I am getting problem. after locking software system is not adding value to that sub-key and giving error that "cannot write to the registry". So where I did mistake, I am unable to find. I checked net also and I found one post here that telling how to edit values of registry sub-keys. I tried that also but problem not rectified.
Здесь я также публикую свой код
//code to lock software after specified date Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings"); if (key != null) { Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other"); if (subkey != null) { 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);//cn is for checking whether locked or not.cn is public variable declared as public void int cn=0 subkey.Close(); uncheck(); } checkService(); getipl(); crelogin(); chklogin(); chkpass(); } public void uncheck() { Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other"); try { var result = (long)subkey.GetValue("InstallDate", "date"); var date = DateTime.FromBinary(result); MessageBox.Show("Install Date" + " " + date.ToString()); var comp = date.AddDays(30);// locking days after installation. var dt = DateTime.Now; //var num = (long)key.GetValue("state", "number"); MessageBox.Show("next Lock Date" + " " + comp.ToString()); //MessageBox.Show("state"+" "+num.ToString()); if (date >= comp) { Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other"); 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();//calling other window to show message this.Close(); } else if (dt >= comp) { Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other"); con.Open(); SqlCommand cmd = new SqlCommand("delete comapny where ctype='b'", con); cmd.ExecuteNonQuery(); con.Close(); //cn = 1; //key.SetValue("number", cn, RegistryValueKind.QWord); //key.Close(); Form2 frm2 = new Form2(); frm2.ShowDialog();//calling other window to show message this.Close(); } else { checkService(); getipl(); crelogin(); chklogin(); chkpass(); } } catch (Exception ex) { MessageBox.Show("Software Is Locked. Contact 9290345689"); this.Close(); } }
Что я уже пробовал:
I checked net also and i found one post here that telling how to edit values of registry sub-keys. I tried that also but problem not rectified.
Sinisa Hajnal
Ты не проверить, если ключ существует способ снять. Идите шаг за шагом, выясните, какая строка вызывает ошибку, и проверьте только эту часть.
vijay_bale
Я проверяю наверху, существует ли ключ или нет. если нет, то я создаю ключ.Если существует,
просто я выхожу из цикла и иду к следующему шагу checkService();