读书人

定时关机的程序异常

发布时间: 2012-03-29 12:53:12 作者: rapoo

定时关机的程序,错误,
我是个初学者,想做一个定时关机的程序,在百度找到了一篇教程文章:http://wenku.baidu.com/view/fa3cb135a32d7375a4178025.html
开始我照着输入进去后,去出现了错误,并且一点效果也没有,着急啊~~希望高手指点一下。
下面是我的代码:

using System;
using System.Collections.Generic;//命名空间包含定义泛型集合的接口和类,用户可以使用泛型集合来创建强类型集合,这种集合能提供比非泛型强类型集合更好的类型安全性和性能
using System.ComponentModel;//命名空间提供用于实现组件和控件运行时和设计时行为的类。此命名空间包括用于实现属性和类型转换器、绑定到数据源以及授权组件的基类和接口
using System.Data;//命名空间提供对表示 ADO.NET 结构的类的访问。通过 ADO.NET 可以生成一些组件,用于有效管理多个数据源的数据。
using System.Drawing;//命名空间提供了对 GDI+ 基本图形功能的访问
using System.Linq;// 命名空间提供支持使用语言集成查询 (LINQ) 进行查询的类和接口
using System.Text;//命名空间包含表示 ASCII 和 Unicode 字符编码的类;用于将字符块转换为字节块和将字节块转换为字符块的抽象基类
using System.Windows.Forms;//命名空间包含用于创建基于 Windows 的应用程序的类,以充分利用 Microsoft Windows 操作系统中提供的丰富的用户界面功能。
using System.Runtime.InteropServices;//提供了一个方法集,这些方法用于分配非托管内存、复制非托管内存块、将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法。
using System.Diagnostics;// 命名空间提供类,使您能够与系统进程、事件日志和性能计数器进行交互。


namespace 定时关机
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
t1.Interval = 1000;
t1.Tick+=new EventHandler(t_Tick);
t1.Start;

}
Timer t1 = new Timer();
void t1_tick(object sender, EventArgs e)
{
LabelNowTime.text = DateTime.Now.ToString();
}
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags,int dwReserved);

private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
{
ExitWindowsEx(0,0);
}
//定义关机方法
private void ShutDown()
{
Process myProcess=new Process();
myProcess.StartInfo.FileName="cmd.exe";
myProcess.StartInfo.UseShellExecute=false;
myProcess.StartInfo.RedirectStandardInput=true;
myProcess.StartInfo.RedirectStandardOutput=true;
myProcess.StartInfo.RedirectStandardError=true;
myProcess.StartInfo.CreateNoWindow=true;
myProcess.Start();//启动进程;
myProcess.StandardInput.WriteLine("shutdown -s -t 0");
}
//定义最小化
private void HideManiForm()
{
this.Hide();
}
//定义显示方法
private void ShowMainForm()
{
this.Show();
this.WindowState=FormWindowState.Normal;
this.Activate();
}

private void 关机ToolStripMenuItem_Click(object sender, EventArgs e)
{
ShutDown();//调用关机方法
}

private void 关机重启ToolStripMenuItem_Click(object sender, EventArgs e)
{
Process myProcess=new Process();
myProcess.StartInfo.FileName="cmd.exe";
myProcess.StartInfo.UseShellExecute=false;
myProcess.StartInfo.RedirectStandardInput=true;
myProcess.StartInfo.RedirectStandardOutput=true;
myProcess.StartInfo.RedirectStandardError=true;
myProcess.StartInfo.CreateNoWindow=true;
myProcess.Start();//启动进程;
myProcess.StandardInput.WriteLine("shutdown -r -t 0");
}

private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();


Application.Exit();
}

private void 重新设定ToolStripMenuItem_Click(object sender, EventArgs e)
{
numericUpDownHour.Value=1;
numericUpDownMinute.Value=1;
numericUpDownSecond.Value=30;
numericUpDownHour.Focus();
Timer.Stop();

}

private void 计时开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
HideMainForm();
}
Timer timer = new Timer();

private void timer_Tick(object sender, EventArgs e)
{
if (numericUpDownSecond.Value == 0)
{
if (numericUpDownMinute.Value == 0)
{
if (numericUpDownHour.Value == 0)
{
timer.Stop();
ShutDown();
}
else
{
numericUpDownHour.Value--;
numericUpDownMinute.Value = 60;
}
}
else
{
numericUpDownMinute.Value--;
numericUpDownSecond.Value = 59;
}
}
else
{
numericUpDownSecond.Value--;
}
}

private void 版本信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("软件名称:定时关机工具\n开发环境:Microsoft Visual Studio 2010\n版本号:H4GRG68X82 (*^__^*)……");
}

private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
HideMainForm();
}

private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if(this.WindowState==FormWindowState.Normal)
{
this.WindowState=FormWindowState.Minimized;
HideMainForm();
}
else if(this.WindowState==FormWindowState.Minimized)
{
ShowMainForm();
}
}
//点击最小化按钮时,最小化到托盘
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
HideMainForm();
}
}

private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowMainForm();
}

private void hideToolStripMenuItem_Click(object sender, EventArgs e)
{
HideMainForm();
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.notifyIcon.Visible = false;
this.Close();
Application.Exit();
}


}
}


[解决办法]
你如果想借鉴别人的程序,你也要找个好的网站或者资源,我不是针对百度文库。
你可以去http://www.cnblogs.com或者csdn mvp的blog里面去学习
参考
http://www.cnblogs.com/wyi23/articles/108270.html


http://blog.csdn.net/greystar/article/details/297363
[解决办法]
呃、、做一个定时关机 有那么复杂?、、
起一个线程 或者用一个timer 计时就可以了
然后事件到了的话 process 启动 cmd 然后 shutdown -s -t 0 就行了 、、

读书人网 >C#

热点推荐