| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2496 人关注过本帖, 1 人收藏
标题:C#有哪个方法能让电脑关机
只看楼主 加入收藏
记忆的持续
Rank: 2
等 级:论坛游民
帖 子:25
专家分:22
注 册:2010-8-21
结帖率:66.67%
收藏(1)
 问题点数:0 回复次数:23 
C#有哪个方法能让电脑关机
怎么样编写一个方法     点击按钮后  电脑就关机       哪位高手帮帮我  谢谢
搜索更多相关主题的帖子: 怎么样 
2010-09-01 14:57
红色警戒
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:19
帖 子:444
专家分:2967
注 册:2005-11-20
收藏
得分:0 
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using using System.Threading;
using System.Diagnostics;
//using System.Configuration;
//using System.Xml ;

——————————————————————————————————————————————

      // 锁定计算机.     
[DllImport("user32.dll")]
private static extern void LockWorkStation();//须写extern
private void button1_Click(object sender, EventArgs e)
      {
         LockWorkStation();
      }
//注销计算机
        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
        private static extern int ExitWindowsEx(int a,int b);
        private void button1_Click(object sender, EventArgs e)
        {
            ExitWindowsEx(0, 0);
        }
//关闭计算机.
private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process mp = new System.Diagnostics.Process();
            mp.StartInfo.FileName = "cmd.exe";
            mp.StartInfo.RedirectStandardInput = true;
            mp.StartInfo.RedirectStandardOutput = true;
            mp.StartInfo.RedirectStandardError = true;
            mp.StartInfo.UseShellExecute = false;
            mp.StartInfo.CreateNoWindow = true;
            mp.Start();//启动进程
            mp.StandardInput.WriteLine("shutdown -s -t 0");//关机
        }
//重启计算机
private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process mp = new System.Diagnostics.Process();
            mp.StartInfo.FileName = "cmd.exe";
            mp.StartInfo.RedirectStandardInput = true;
            mp.StartInfo.RedirectStandardOutput = true;
            mp.StartInfo.RedirectStandardError = true;
            mp.StartInfo.UseShellExecute = false;
            mp.StartInfo.CreateNoWindow = true;
            mp.Start();//启动进程
            mp.StandardInput.WriteLine("shutdown -r -t 0");//重启
        }

——————————————————————————————————————————

使计算机开机时就会执行自动关机

using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using using System.Threading;
using System.Diagnostics;
//using System.Configuration;
//using System.Xml ;


namespace _124516
{
    public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("user32.dll")]
        private static extern void LockWorkStation();//须写extern
        private void Form1_Load(object sender, EventArgs e)
        {
            RegistryKey KeyCon = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            string MyKey = "124516.exe";
            if ((string)KeyCon.GetValue(MyKey, "no") == "no")//指定的键不存在
            {

                string Path = Application.StartupPath + @"\124516.exe"; KeyCon.SetValue(MyKey, Path);
                //System.Diagnostics.Process mp = new System.Diagnostics.Process();
                //mp.StartInfo.FileName = "cmd.exe";
                //mp.StartInfo.RedirectStandardInput = true;
                //mp.StartInfo.RedirectStandardOutput = true;
                //mp.StartInfo.RedirectStandardError = true;
                //mp.StartInfo.UseShellExecute = false;
                //mp.StartInfo.CreateNoWindow = true;
                //mp.Start();//启动进程
                //mp.StandardInput.WriteLine("shutdown -s -t 0");

                //设置注册表中的启动键
            }
            else
            {
                //System.Diagnostics.Process mp = new System.Diagnostics.Process();
                //mp.StartInfo.FileName = "cmd.exe";
                //mp.StartInfo.RedirectStandardInput = true;
                //mp.StartInfo.RedirectStandardOutput = true;
                //mp.StartInfo.RedirectStandardError = true;
                //mp.StartInfo.UseShellExecute = false;
                //mp.StartInfo.CreateNoWindow = true;
                //mp.Start();//启动进程
                //mp.StandardInput.WriteLine("shutdown -s -t 0");
            }


2010-09-01 15:08
SUPERJHT
Rank: 1
等 级:新手上路
帖 子:18
专家分:8
注 册:2010-7-19
收藏
得分:0 
好!谢谢。我正需要哪
2010-09-01 15:56
记忆的持续
Rank: 2
等 级:论坛游民
帖 子:25
专家分:22
注 册:2010-8-21
收藏
得分:0 
回复 楼主 记忆的持续
谢谢了  我正在试
2010-09-01 16:09
xiaxun
Rank: 1
等 级:新手上路
帖 子:207
专家分:7
注 册:2009-8-30
收藏
得分:0 
好东西呀
2010-09-01 17:41
ConfusingMan
Rank: 2
等 级:论坛游民
帖 子:5
专家分:10
注 册:2010-9-1
收藏
得分:0 
兄弟们太厉害了
2010-09-01 21:57
qingshuiliu
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:17
帖 子:323
专家分:1538
注 册:2009-12-28
收藏
得分:0 
不错,不错,学习
2010-09-02 12:37
playermaker
Rank: 2
等 级:论坛游民
威 望:1
帖 子:87
专家分:17
注 册:2008-8-15
收藏
得分:0 
来晚了,呵呵
要操作API函数

[url=http://www.]优联(Union)官方网站[/url]
2010-09-02 13:39
lach
Rank: 2
等 级:论坛游民
帖 子:26
专家分:31
注 册:2010-8-30
收藏
得分:0 
学习了
2010-09-02 16:15
sgrrzbc
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-8-14
收藏
得分:0 
学习中~~
2010-09-02 16:41
快速回复:C#有哪个方法能让电脑关机
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023323 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved