QQDownload关机后关闭系统代码

     QQDownload 其自身带有“完成后关机”的功能,然而 完成下载后,只是关掉自身的进程,实现关闭程序,并未关机。完成后系统却还开着,浪费电。

如何实现其自动关机,小虾自己做了一个程序,解决了这个问题。

整体思路是这样:

1,启动关机 程序;

2,检测QQDownload是否运行;

3,完成后关机。

下面附出实现代码,可能比较冗余,请大家见谅。

1GetSysProcess.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ShotDownSys
{
    class GetSysProcess
    {
        private List GetSysProc()
        {
            Process[] Curprocess = Process.GetProcesses();
            List pList = new List();//定义一个List
            foreach (Process p in Curprocess)
            {
                pList.Add(p.ProcessName.ToString());
            }
            return pList;
        }
        //获取字符Array
        public List SysProcInfo()
        {
            return GetSysProc();
        }
        public string[] SysProcInfo_String()
        {
            Process[] Curprocess = Process.GetProcesses();
            string[] pList = new string[Curprocess.Length];
            for (int i = 0; i < Curprocess.Length; i++)
            {
                pList[i] = Curprocess[i].ProcessName;
            }
            return pList;
        }
    }
}

 

Form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ShotDownSys
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DateTime dt = Convert.ToDateTime("00:00:59");
        private GetSysProcess gsp;
        //private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        private List pList;
        private string[] pstr;
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Enabled = false;
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            comboBox1.Enabled = true;
            FillControl();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //gsp = new GetSysProcess();
            //pstr = gsp.SysProcInfo_String();
            //contextMenu1.MenuItems.Add(this.MenuItem1.);
            this.contextMenu1.MenuItems.Add(this.menuItem2.CloneMenu());
            this.contextMenu1.MenuItems.Add(this.menuItem3.CloneMenu());
            this.notifyIcon1.ContextMenu = this.contextMenu1;
            FillControl();

        }
        private void FillControl()
        {
            gsp = new GetSysProcess();
            pstr = gsp.SysProcInfo_String();
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(pstr);
           
            listBox1.Items.Clear();
            listBox1.Items.AddRange(pstr);
        }
        private void start()
        {
            if (comboBox1.Text.Trim() == "")
            {
                MessageBox.Show("请选择进程");
                return;
            }
            string pSelect = comboBox1.Text;
            gsp = new GetSysProcess();
            pList = gsp.SysProcInfo();
            int i = pList.FindIndex(delegate(string str) { return str == pSelect; });
            //MessageBox.Show(i.ToString());
            if (i == -1)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            FillControl();
           // MessageBox.Show("OK");
            start();
        }
        private void ShutDownSys()
        {

            if (dt != Convert.ToDateTime("00:00:00"))
            {
                this.Text = dt.Second.ToString() + "Second Left!";
                //MessageBox.Show(dt.Second.ToString()+"Second Left!");
                dt = dt.AddSeconds(-1);
            }
            else
            {
                DoExitWin(EWX_SHUTDOWN);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            ShutDownSys();
        }


        [StructLayout(LayoutKind.Sequential, Pack = 1)]

        internal struct TokPriv1Luid
        {
            public int Count;

            public long Luid;

            public int Attr;

        }

        [DllImport("kernel32.dll", ExactSpelling = true)]

        internal static extern IntPtr GetCurrentProcess();

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

        [DllImport("advapi32.dll", SetLastError = true)]

        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,

         ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

        internal static extern bool ExitWindowsEx(int flg, int rea);

        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

        internal const int TOKEN_QUERY = 0x00000008;

        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

        internal const int EWX_LOGOFF = 0x00000000;

        internal const int EWX_SHUTDOWN = 0x00000001;

        internal const int EWX_REBOOT = 0x00000002;

        internal const int EWX_FORCE = 0x00000004;

        internal const int EWX_POWEROFF = 0x00000008;
       internal const int EWX_FORCEIFHUNG = 0x00000010;
        private void DoExitWin(int flg)
        {

            bool ok;

            TokPriv1Luid tp;

            IntPtr hproc = GetCurrentProcess();

            IntPtr htok = IntPtr.Zero;

            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
            tp.Count = 1;
            tp.Luid = 0;
            tp.Attr = SE_PRIVILEGE_ENABLED;
            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
            ok = ExitWindowsEx(flg, 0);
        }

        private void Form1_MinimumSizeChanged(object sender, EventArgs e)
        {
            MessageBox.Show("OK");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Hide();
            this.notifyIcon1.Visible = true;
        }
        //
        private void MenuItem1_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Hide();
            this.notifyIcon1.Visible = true;
        }
        //
        private void MenuItem2_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.Show();
            this.notifyIcon1.Visible = false;
        }

        private void menuItem2_Click_1(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Hide();
            this.notifyIcon1.Visible = true;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.Show();
            this.notifyIcon1.Visible = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
}

你可能感兴趣的:(QQDownload关机后关闭系统代码)