在C#中指定时间后锁定我的软件 [英] Locking my software after specified time in C#

查看:97
本文介绍了在C#中指定时间后锁定我的软件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码,用于在指定时间后锁定我的软件。为此,我使用了注册表。

我创建了一个子密钥并检查我的软件是否第一次运行。如果我第一次创建子密钥并存储安装日期。我正在读取该日期并添加一些日期并在每次用户打开我的软件时进行检查。这里一切正常,但是如果用户更改日期并恢复数据,锁定软件后,我无法跟踪。所以我添加了其他子密钥,当我锁定软件时,我用1替换该子密钥值。我想要检查已经锁定过的软件,所以如果他恢复数据并更改了日期,我也可以锁定软件。这里只有我遇到问题。锁定后软件系统没有为该子键添加值并给出无法写入注册表的错误。所以我错了,我找不到。我也检查了网络,我在这里发现了一个帖子,告诉我们如何编辑注册表子键的值。我试过了,但问题没有解决。



我在这里发布我的代码



I wrote some code to lock my software after specified time. For that I used registry.
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.

Here I am posting my code also

//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();
            }
        }





我的尝试:





What I have tried:

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.

推荐答案

规则一:停止使用注册表。

访问注册表比以前更受限制,并且可能会变得更加严格。而且您的问题很可能是您尝试编辑的注册表部分需要管理员访问权限,这意味着使用阿联酋并获得用户的许可以提升您的应用程序。



相反,请考虑使用配置文件,您可以根据需要添加,删除和编辑条目。
Rule one: stop using the registry.
Access to the registry is more restricted than it used to be, and likely to become more so. And your problem is most likely to be that the part of the registry you are trying to edit needs Admin access, which means using UAE and getting permission from the user to elevate your app.

Instead, consider using a configuration file where you can add, delete, and edit entries as needed.


您必须先关闭密钥才能重新打开它们。在你的代码中,你永远不会关闭SOFTWARE\OurSettings的关键字(它不关心,因为它不在任何地方使用,但也应该关闭)。这同样适用于上面刚刚打开的SOFTWARE \OurSettings \ other键。然后,您正在调用 uncheck(),它会再次尝试打开相同的密钥。在创建密钥时,您可以在调用 uncheck()之前关闭密钥。



始终关闭不使用anmyore的钥匙。对于您的示例,您也可以将键作为参数传递给 uncheck()函数,然后将其关闭。





如果要写入注册表,则必须打开具有写访问权限的子项。具有单个参数的实际使用的方法将打开子键只读。请参阅 RegistryKey.OpenSubKey方法(Microsoft.Win32) [ ^ ]用于具有访问参数的方法。



我还注意到你在<$上打开了相同的子键c $ c> uncheck()作为运行,然后再作为子键再次运行。



另请注意,删除密钥可以简单地规避试用期结束。

[/ EDIT]
You have to close keys before they can be re-opened. In your code you are never closing the key to "SOFTWARE\OurSettings" (which does not care because it is not used anywhere but should be also closed). The same applies to the key for "SOFTWARE\OurSettings\other" opened just after the above. You are then calling uncheck() which tries to open the same key again. When creating the key, you do it right by closing the key before calling uncheck().

Always close keys when not used anmyore. For your example you might also pass the key as argument to the uncheck() function and close it afterwards.


If you want to write to the registry, you have to open the subkey with write access. The actually used method with a single parameter will open the subkey read only. See RegistryKey.OpenSubKey Method (Microsoft.Win32)[^] for methods that have access parameters.

I just noted also that you are opening the same subkey on top of the uncheck() function as key and then again later as subkey.

Note also that such end of trial period detections can be simply circumvented by deleting the keys.
[/EDIT]


我找到了解决方案并纠正了问题。感谢jochen和OriginalGriff。



以下我粘贴完整的最终代码。也许它对某些人有用。



我在net中找到了一个解决方案,他们告诉你在opensubkey时添加true。我添加并解决了我的问题



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

//code to lock software after specified date
            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);//specifing days after inatallation date to lock
                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;//already define as public. (public static int cn=0;)
                    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)
                {
                    
                    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 (stat!= 0)
                {
                    
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  comapny where ctype='b'", con);
                    cmd.ExecuteNonQuery();
                    //Not calling any message window
                    con.Close();
                    subkey.Close();

                }

                else
                {

                    checkService();
                    getipl();
                    crelogin();
                    chklogin();
                    chkpass();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Software Is Locked. Contact 9290345689");
                this.Close();
            }
        }


这篇关于在C#中指定时间后锁定我的软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆