首页 新闻 赞助 找找看

c#写的软件的bug

0
悬赏园豆:5 [已解决问题] 解决于 2012-04-26 20:36
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;
using System.IO;
using System.IO.Compression;

namespace shutdown
{
public partial class Form1 : Form
{
private static string PathBase = Application.StartupPath; //获取应用程序启动路径
string ShutDownTime = "";
public Form1()
{
InitializeComponent();
}


private void 定时关机ToolStripMenuItem_Click(object sender, EventArgs e)
{
关机时间toolStripTextBox1.Text = DateTime.Now.ToLongTimeString ();
关机时间toolStripTextBox1.Visible = true;
}

protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Visible = false;
base.OnClosing(e);
}

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

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

p.Start(); //啟動
}
private void 关机时间toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox1.Text != "")
{
try
{
DateTime time = DateTime.Parse(关机时间toolStripTextBox1.Text);
TimeSpan span = new TimeSpan(time.Hour, time.Minute, 0);
if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) == 0) //当前关机时间
{
DialogResult result = MessageBox.Show("是否现在关机!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result == DialogResult.OK)
{
//MynotifyIcon.Icon = Icon.ExtractAssociatedIcon(PathBase + "\\Images\\" + "ShutDown_notifyIcon.ico");
//
//调用关机程序
//
CloseExit();

}
else
{
return;
}
}
if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) < 0) //过去关机时间
{
MessageBox.Show("此时间已是过去时间,设定无效", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (span.CompareTo(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0)) > 0) //有效关机时间
{

ShutDownTime = 关机时间toolStripTextBox1.Text;
MessageBox.Show("成功设置定时关机,计算机将于“" + ShutDownTime + "”关机…… *︶︵︶*", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.关机时间toolStripTextBox1.Visible = false;
CloseExit();

//
//调用关机程序
//
}
}
catch
{
MessageBox.Show("日期格式不正确,请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
if (e.KeyChar == (char)Keys.Enter && 关机时间toolStripTextBox1.Text == "")
{
关机时间toolStripTextBox1.Visible = false;
}
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否现在关机!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (result == DialogResult.Yes)
{
CloseExit();
}
}
}
}

会当凌绝顶的主页 会当凌绝顶 | 初学一级 | 园豆:189
提问于:2011-12-20 23:24
< >
分享
最佳答案
0

button1_Click能实现关机啊,如果是关机时间toolStripTextBox1_KeyPress()我倒是看到个问题

DialogResult result = MessageBox.Show("是否现在关机!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);  if (result  == DialogResult.OK)  应该是DialogResult.Yes……

收获园豆:5
yanzhe | 菜鸟二级 |园豆:213 | 2011-12-23 13:53

哈哈,不错不错...

宇宙工作室 | 园豆:205 (菜鸟二级) | 2011-12-23 15:22

额?我的button1_click不能实现关机,反而弹出一个和form一样的窗口,这是什么原因啊?还有,我的代码是dialogresult.yes啊

会当凌绝顶 | 园豆:189 (初学一级) | 2011-12-24 16:25

@会当凌绝顶: 不是吧?我用了你的方法怎么可以实现啊……不行用断点试试吧,看看哪儿有问题!

yanzhe | 园豆:213 (菜鸟二级) | 2011-12-26 09:34
其他回答(2)
0

1.有没有执行CloseExit 方法;

2.你在自己机器上面运行shutdown -s -t 0 可以关机不

小小刀 | 园豆:1991 (小虾三级) | 2011-12-20 23:56

closeexit方法可以关机,我专门试过。还有为什么点击现在关机会弹出一个和form一样的窗体?

支持(0) 反对(0) 会当凌绝顶 | 园豆:189 (初学一级) | 2011-12-21 17:28
0

看这个代码;有源代码下载下来研究吧;http://www.cnblogs.com/flydoos/archive/2011/08/20/2146472.html

画方软件 | 园豆:778 (小虾三级) | 2011-12-21 17:25

我现在主要是想知道我的代码有什么问题?

支持(0) 反对(0) 会当凌绝顶 | 园豆:189 (初学一级) | 2011-12-21 17:29

@会当凌绝顶: 你的代码我没有看,不过你把我发你的下载下来看看吧,把他的关机代码弄过来去;你的应该是关机代码有问题;那调用进程的不怎么好;最好用api操作;

支持(0) 反对(0) 画方软件 | 园豆:778 (小虾三级) | 2011-12-21 17:31

@HuaFang: 能帮忙看一下么,你推荐的那个代码我下了,但是我代码中的关机代码单独运行是可以成功。

支持(0) 反对(0) 会当凌绝顶 | 园豆:189 (初学一级) | 2011-12-21 21:55

@会当凌绝顶:不会吧;你没有看到下载链接吗;你自己下载下来吧; 

支持(0) 反对(0) 画方软件 | 园豆:778 (小虾三级) | 2011-12-21 21:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册