帮朋友做的限制小孩上网时间软件的源码
昨天帮朋友做一个限制他们家小孩上网时间的软件,觉得很有意义,花了两个小时,很简单,还挺管用,针对只会玩游戏的小孩够用了!呵呵。
下面是源文件下载地址:
https://files.cnblogs.com/mane/WINUSER.zip
XML文件,(应该加密帐号密码的,太麻烦,就算了)
<?xml version="1.0" encoding="utf-8" ?> <Users> <!--记录用户的帐号,密码,已经可以使用的时间--> <User name="Admin" pwd="123456" role="管理员" date="2012-07-02">10</User> <User name="User" pwd="123456" role="" date="2012-07-02">10</User> </Users>
操作XML的类 (别见怪,重复利用率是个问题)
using System; using System.Collections.Generic; using System.Text; using System.Xml;//引入XML命名空间 namespace WINUSER { class ControlXML { private string filePath; public ControlXML(string XMLFilePath) { filePath = XMLFilePath; } /// <summary> /// 获取时间值 /// </summary> /// <param name="userName"></param> /// <param name="userPwd"></param> /// <returns></returns> public string GetValue(string userName, string userPwd) { string value=""; XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(filePath);//读取XML文档 XmlNode xn = XMLDoc.SelectSingleNode("Users"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { if (xnf.Name == "User") { XmlElement sonData = (XmlElement)xnf; if (sonData.GetAttribute("name") == userName && sonData.GetAttribute("pwd") == userPwd) { value = sonData.InnerText; } } } return value; } /// <summary> /// 获取日期 /// </summary> /// <param name="userName"></param> /// <param name="userPwd"></param> /// <returns></returns> public DateTime GetData(string userName, string userPwd) { DateTime dt=new DateTime (); XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(filePath);//读取XML文档 XmlNode xn = XMLDoc.SelectSingleNode("Users"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { if (xnf.Name == "User") { XmlElement sonData = (XmlElement)xnf; if (sonData.GetAttribute("name") == userName && sonData.GetAttribute("pwd") == userPwd) { dt = Convert.ToDateTime(sonData.GetAttribute("date")); } } } return dt; } /// <summary> /// 获取用户规则 /// </summary> /// <param name="userName"></param> /// <param name="userPwd"></param> /// <returns></returns> public string GetRole(string userName, string userPwd) { string role = ""; XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(filePath);//读取XML文档 XmlNode xn = XMLDoc.SelectSingleNode("Users"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { if (xnf.Name == "User") { XmlElement sonData = (XmlElement)xnf; if (sonData.GetAttribute("name") == userName && sonData.GetAttribute("pwd") == userPwd) { role = sonData.GetAttribute("role"); } } } return role; } /// <summary> /// 写入日期 /// </summary> /// <param name="userName"></param> /// <param name="userPwd"></param> public void WriteData(string userName, string userPwd) { XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(filePath);//读取XML文档 XmlNode xn = XMLDoc.SelectSingleNode("Users"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { if (xnf.Name == "User") { XmlElement sonData = (XmlElement)xnf; if (sonData.GetAttribute("name") == userName && sonData.GetAttribute("pwd") == userPwd) { sonData.SetAttribute("date", DateTime.Now.ToShortDateString());//设置为当前日期 } } } XMLDoc.Save(filePath); } /// <summary> /// 写入时间值 /// </summary> /// <param name="userName"></param> /// <param name="userPwd"></param> public void WriteValue(string userName, string userPwd,string lastTime) { XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(filePath);//读取XML文档 XmlNode xn = XMLDoc.SelectSingleNode("Users"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl) { if (xnf.Name == "User") { XmlElement sonData = (XmlElement)xnf; if (sonData.GetAttribute("name") == userName && sonData.GetAttribute("pwd") == userPwd) { sonData.InnerText = lastTime;//设置剩余的时间值 } } } XMLDoc.Save(filePath); } } }
主要界面(设置为不能在最小化任务栏显示,并且隐藏主窗体,防止关闭,但可以在任务管理器关闭,所有必须禁用任务管理器)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32; namespace WINUSER { public partial class Main : Form { public static Main main; public Main() { InitializeComponent(); main = this; } /// <summary> /// 剩余的时间 /// </summary> public static int lastTime = 14400; /// <summary> /// 当前登录用户的帐号,密码 /// </summary> public static string userName, userPwd; /// <summary> /// 初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Main_Load(object sender, EventArgs e) { //隐藏主窗体,防止用户关闭 this.Visible = false; this.WindowState = FormWindowState.Minimized; //设置为开机启动,每次开机都自动运行该程序 RegistryKey hkml = Registry.LocalMachine; RegistryKey runKey = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); runKey.SetValue("WinUser", Application.StartupPath + "\\WINUSER.exe"); runKey.Close(); //打开用户登录 Login login = new Login(); login.ShowDialog(); } /// <summary> /// 设定计时器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { lastTime--; if (lastTime <= 0) { //关闭计时器 timer1.Enabled = false; //重新登录 Login login = new Login(); login.ShowDialog(); } //保存当前剩余时间 ControlXML cx = new ControlXML(Application.StartupPath + "\\User.xml"); cx.WriteValue(userName, userPwd, lastTime.ToString());//设置新的上网时间值 } } }
登录界面(需要禁用任务管理器)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Win32;// namespace WINUSER { public partial class Login : Form { public Login() { InitializeComponent(); } /// <summary> /// 初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Login_Load(object sender, EventArgs e) { //作为顶层窗口,和最大化窗口,屏蔽其他窗口 this.TopMost = true;//顶层窗口 this.WindowState = FormWindowState.Maximized;//最大化窗口 //停用任务管理器,删除该注册表即可解除 RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true); r.SetValue("DisableTaskMgr", "1"); //屏蔽任务管理器 } /// <summary> /// 阻止程序关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Login_FormClosing(object sender, FormClosingEventArgs e) { //防止意外关闭本窗口 e.Cancel = true;//防止关闭窗口 } /// <summary> /// 登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { ControlXML cx = new ControlXML(Application.StartupPath + "\\User.xml"); //如果用户输入成功登录 string value = cx.GetValue(comboBox1.Text, textBox1.Text); if (value != null && value != "") { //如果当前日期和保存日期不一致则说明是新的一天,刷新最后一次使用日期和新的上网时间值 DateTime dt = cx.GetData(comboBox1.Text, textBox1.Text); if (DateTime.Now.ToShortDateString() != dt.ToShortDateString()) { cx.WriteData(comboBox1.Text, textBox1.Text);//设置新的日期值 cx.WriteValue(comboBox1.Text, textBox1.Text,"14400");//设置新的上网时间值为4小时 } //获取用户规则如果为管理员则一天都可以上网 string role = cx.GetRole(comboBox1.Text, textBox1.Text); if (role == "管理员") { cx.WriteValue(comboBox1.Text, textBox1.Text, "86400");//设置新的上网时间值为24小时 } //获取上网时间值,并关闭本窗口 Main.lastTime = int.Parse(cx.GetValue(comboBox1.Text, textBox1.Text)); Main.userName = comboBox1.Text; Main.userPwd = textBox1.Text; this.Visible = false; //打开计时器 Main .main .timer1.Enabled = true; MessageBox.Show("你还有" + Main.lastTime.ToString() + "秒的上网时间可用!"); } else { MessageBox.Show("密码错误!"); } } /// <summary> /// 关机 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { WindowsExit.ExitWin(); } } }