首页 | 互联网 | IT动态 | Cisco | Windows | Linux | Java | .Net | Oracle | 华为 | 存储世界 | 服务器 | 网络设备 | IDC | 安全 | 求职招聘
IT培训 | 数字网校 | 技术专题 | 电子书下载 | 教学视频 | 网页设计 | 平面设计 | 解决方案 | 直播室 | 虚拟考场 | 搜索 | 博客 | 沙龙 | 论坛
中国IT实验室Dotnet频道
中国IT教育
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 文章正文

C#写的自动关机及执行CMD命令

文章来源中国IT实验室收集整理 作者佚名 更新时间2008-5-12 保存本文保存本文 推荐给好友推荐给好友 收藏本页收藏本页

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

    namespace WindowsApplication1
    ...{
        public partial class Form1 : Form
        ...{
        int i = 0;
        private int ti;
        public Form1()
        ...{
            InitializeComponent();
            timer1.Enabled = false;
        }
        public string CmdPc(string cmdinput)
        ...{
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            try
            ...{
                p.Start();
                p.StandardInput.WriteLine(cmdinput);
                p.StandardInput.WriteLine("exit");
                string ss=p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
                return ss;
            }
            catch
            ...{
                string ss = "命令执行失败";
                return ss;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        ...{
            //label1.Text = ClosePc();
            if (textBox2.Text != "")
            ...{
                ti = Convert.ToInt32(textBox2.Text);
                timer1.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button1.Enabled = false;

            }
            else ...{ label4.Text = "请输入时间,单位/分钟"; }
        }

        private void timer1_Tick(object sender, EventArgs e)
        ...{

            if (ti - i == 1)//从5开始到计时
            ...{
                timer1.Enabled = false;
                label4.Text = "时间到,关机!!";
                label1.Text = CmdPc("Shutdown.exe -s -f");
                button4.Visible = true;
            }
            else
            ...{
                i++;
                string j = (ti - i).ToString();
                label4.Text = j;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        ...{
            timer1.Enabled = true;
            button4.Enabled = true;
            button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        ...{
            label1.Text=CmdPc(textBox1.Text);
        }
        private void button4_Click_1(object sender, EventArgs e)
        ...{
            timer1.Enabled = false;
            button4.Enabled = false;
            button1.Enabled = true;
            label4.Text = "停止";
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        ...{
            if (e.KeyChar == 13)
            ...{
                ti = Convert.ToInt32(textBox2.Text);
                timer1.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
                button1.Enabled = false;
            }
        }

 

    }
}

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题
 ·展现C#世界 C#程序设计专题
 ·Java入门 Tomcat的配置技巧精华专题
 ·Oracle RMAN物理备份技术详解
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航