社区
C#
帖子详情
如何用C#命令关机?
yuhao210
2006-01-23 07:28:51
我用using System.Runtime.InteropServices ;
using System.Text ;
[ DllImport("user32") ]
public static extern long ExitWindowsEx(long uFlags, long dwReserved ) ;
ExitWindowsEx(1, 0);
怎么没有反应
用ExitWindowsEx(0, 0);是注销
...全文
411
7
打赏
收藏
如何用C#命令关机?
我用using System.Runtime.InteropServices ; using System.Text ; [ DllImport("user32") ] public static extern long ExitWindowsEx(long uFlags, long dwReserved ) ; ExitWindowsEx(1, 0); 怎么没有反应 用ExitWindowsEx(0, 0);是注销
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lengyu633
2006-01-24
打赏
举报
回复
up
aSalt
2006-01-24
打赏
举报
回复
see WMI functions
yf1025
2006-01-23
打赏
举报
回复
mark
DrSmart
2006-01-23
打赏
举报
回复
在2000以后你要获取SeShutdownPrivilege权限
E_BADBOY
2006-01-23
打赏
举报
回复
C#调用API关机:
namespace ShutDownTest {
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Security.Principal;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
};
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct TOKEN_PRIVILEGES
{
public uint PrivilegeCount;
public LUID Luid;
public uint Attributes;
};
public class PlatformImport
{
[DllImport("advapi32", CharSet=CharSet.Auto)]
public static extern bool LookupPrivilegevalue (string sysname,string privname,ref LUID luid);
[DllImport("advapi32", CharSet=CharSet.Auto)]
public static extern bool AdjustTokenPrivileges(IntPtr handle, bool dsall,ref TOKEN_PRIVILEGES newstate,int len, IntPtr oldstate,IntPtr retlen);
[DllImport("kernel32.dll")]
public static extern int GetLastError();
[DllImport("user32.dll")]
public static extern bool ExitWindowsEx(int uFlags, int dwReason);
}
public class Form1 : Form
{
private Button button1;
public Form1()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new Point(136, 112);
this.button1.Name = "button1";
this.button1.Size = new Size(80, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Shut Down";
this.button1.Click += new EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.Controls.Add(button1);
this.Name = "Form1";
this.Text = "shutdown test";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
IntPtr token = identity.Token;
IntPtr luid = (IntPtr)0;
IntPtr previousState = (IntPtr)0;
IntPtr previousStateLength = (IntPtr)0;
LUID privilegeId = new LUID ();
PlatformImport.LookupPrivilegevalue ("", "SeShutdownPrivilege", ref privilegeId);
TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES();
privileges.PrivilegeCount = 1;
privileges.Luid = privilegeId;
privileges.Attributes = 2;
PlatformImport.AdjustTokenPrivileges (token, false, ref privileges, Marshal.SizeOf(privileges), previousState, previousStateLength);
if (PlatformImport.GetLastError() != 0) return;
if(!(PlatformImport.ExitWindowsEx(0x01 | 0x04, 0))) return;
}
}
}
rainnight83
2006-01-23
打赏
举报
回复
楼主可以试试在c#里面执行cmd命令
命令为shutdown -s
偶没试过哈
只知道C#可以跑cmd命令
califord
2006-01-23
打赏
举报
回复
我手上到是有一个,也是网上下的,记得以前我发过贴,你找一下好了
c#
使用shutdown
关机
命令
让xp和vista
关机
c#
使用shutdown
关机
命令
让xp和vista
关机
C#
实现执行CMD
命令
,实现电脑立即/定时
关机
该软件实现
C#
调用系统cmd.exe,并实现cmd
命令
。
C#
Winform自带的RDLC报表实例教程
以实例讲解
C#
Winform自带的RDLC报表的基本设置,格式化,排序与分组,常用报表控件的使用,子报表、主从报表、钻取报表的设计方法等
c#
写的一个远程
关机
c#
写的一个远程
关机
只需在代码中修改IP地址就可以关掉对方的机子在学校试过!!呵呵!!!希望对大家有用!!
C#
使用Dos
命令
实现
关机
和重启
使用Dos
命令
实现
关机
和重启 程序源代码
C#
110,534
社区成员
642,575
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章