频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源:中国IT实验室 作者:佚名 出处:巧巧读书 2008-05-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;
            }
        }

    }
}

更多文章 更多内容请看Linux命令简介专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章