由于Q+桌面和QQ同时在线5小时可加速0.2天,
没事做了本程序当练手用,实现自动监控QPLUS到5小时后关闭.
本程序为控制台程序,启动后在后台自动运行、有系统托盘并设置了开机启动
功能
1.如本程序运行时,Q+桌面还未运行,将自动运行Q+桌面
2.自动监控QQ是否关联到Q+,如果Q+桌面没有关联到QQ帐号,5分钟提醒一次(只提醒3次,提醒太多会很烦人的)
3.Q+桌面和QQ同时在线5小时后即自动退出.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Timers; using Microsoft.Win32; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Threading; //确定QPLUS已经运行5小时就自动关闭QPLUS namespace SHUTQQPLUS { class Program { static TimeSpan waste = new TimeSpan(0);//未关联的时间 static string filePath = "";//Q+安装路径 static int iflag = 0;//关联标志 public static int icount = 3;//提醒次数 static System.Timers.Timer Timer2 = new System.Timers.Timer(); //定时器2 //实现系统托盘 static NotifyIcon _NotifyIcon = new NotifyIcon(); static void Main(string[] args) { //防止多个程序运行 Process[] ps = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); if (ps.Length > 1) { MessageBox.Show("程序已运行!"); return; } try { if (checkInstall() == false) { MessageBox.Show("Q+桌面未安装!"); return;//终止程序 } MessageBox.Show("注意:程序将自动在后台运行!"); Process[] processes = Process.GetProcessesByName("qplus"); while(processes.Length == 0) { //自动运行Q+桌面 ProcessStartInfo p = new ProcessStartInfo(); p.FileName = filePath; p.WindowStyle = ProcessWindowStyle.Normal; Process pr = Process.Start(p); processes = Process.GetProcessesByName("qplus"); } //设置程序为开机启动 #region RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 if ((key.GetValue("SHUTQQPLUS") == null))//判断是否已经是开机启动 { if (System.Environment.Is64BitOperatingSystem == true)//64位系统 { key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 if (key == null)//如果该项不存在的话,则创建该子项 { key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run"); } } else { if (key == null)//如果该项不存在的话,则创建该子项 { key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); } } key.SetValue("SHUTQQPLUS", Environment.CommandLine);//设置为开机启动 key.Close(); } #endregion System.Timers.Timer Timer1 = new System.Timers.Timer(); //定时器1 Timer1.Elapsed += new ElapsedEventHandler(TimeEvent); Timer1.Interval = 3600000;//设置间隔时间为1小时 Timer1.Enabled = true; Timer2.Elapsed += new ElapsedEventHandler(TimeEvent2); Timer2.Interval = 30000;//设置间隔时间为5分钟 Timer2.Enabled = true; ShowNotifyIcon();//设置系统托盘 _NotifyIcon.MouseDoubleClick += new MouseEventHandler(_NotifyIcon_MouseDoubleClick); _NotifyIcon.Click += new System.EventHandler(_NotifyIcon_Click); while (true) { Application.DoEvents(); }; } catch { MessageBox.Show("出现未知错误"); } } private static void TimeEvent(object source, ElapsedEventArgs e) //定时器时间 检测是否满5小时 { try { Process[] processes = Process.GetProcessesByName("qplus"); DateTime now = System.DateTime.Now; if (1 == iflag) { foreach (Process p in processes) { foreach (ProcessModule fp in p.Modules) { if (fp.ModuleName.ToString() == "TXPFProxy.dll")//判断是否关联到QQ的进程 { if ((now - p.StartTime - waste).Hours.CompareTo(5) >= 0)//判断运行时间大于5小时 { p.Kill(); MessageBox.Show(p.ProcessName + "[" + p.Id + "]:" + "成功关闭!"); Environment.Exit(0);//关闭Q+后终止程序 } } } } } else { return;//没有关联的话 不去检测是否大于5小时 } } catch { MessageBox.Show("关闭进程qplus失败,出现错误!"); } } private static void TimeEvent2(object source, ElapsedEventArgs e) //定时器时间 每5分钟检测一次Q+是否已经关联到QQ { if (1 == iflag) Timer2.Enabled = false;//如果已经关联了,则关闭定时器2 try { Process[] processes = Process.GetProcessesByName("qplus"); DateTime now = System.DateTime.Now; if (iflag == 0) { foreach (Process p in processes) { foreach (ProcessModule fp in p.Modules) { if (fp.ModuleName.ToString() == "TXPFProxy.dll")//判断是否关联到QQ的进程 { iflag = 1; waste = System.DateTime.Now - p.StartTime;//没关联之前的时间不算 } } if (iflag == 0 && icount != 0) { icount--; MessageBox.Show(p.ProcessName + "没有关联到QQ帐号,请尽快将Q+桌面关联到QQ帐号!"); } } } } catch { MessageBox.Show("关闭进程qplus失败,出现错误!"); } } //实现系统托盘 public static void ShowNotifyIcon() { _NotifyIcon.Icon = new Icon(System.Windows.Forms.Application.StartupPath + "\\logo_16.ico");//系统托盘图标 _NotifyIcon.Visible = false; _NotifyIcon.Text = "Q+运行监控中..."; ContextMenu menu = new ContextMenu(); MenuItem item = new MenuItem(); item.Text = "退出"; item.Index = 0; menu.MenuItems.Add(item); _NotifyIcon.ContextMenu = menu; _NotifyIcon.Visible = true; _NotifyIcon.ShowBalloonTip(3000, "", "程序已运行!", ToolTipIcon.None); } public static void HideNotifyIcon() { _NotifyIcon.Visible = false; } private static void _NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) { MessageBox.Show("程序已运行!!!"); } private static void _NotifyIcon_Click(object sender, EventArgs e) { if (MessageBox.Show("确定退出程序?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) Environment.Exit(0);//终止程序 } //检查QPLUS是否安装 private static bool checkInstall() { Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"); if (System.Environment.Is64BitOperatingSystem == true)//64位系统 uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"); foreach (string subKeyName in uninstallNode.GetSubKeyNames()) { Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName); object displayName = subKey.GetValue("DisplayName"); if (displayName != null) { if (displayName.ToString().Contains("Q+桌面")) { filePath = subKey.GetValue("InstallLocation").ToString() + "\\QPlus.exe"; return true; } } } return false; } } }
下载地址:
http://pan.baidu.com/share/link?shareid=491334&uk=2449788611
纯粹为了练习C#而写的,大神别笑!
--
请注意:现在已经没有"Q+桌面和QQ同时在线5小时可加速0.2天"的QQ等级加速了。