[VB.NET]VB.NET怎样设计定时关机???

VB.NET源码-156个实用实例哦…… <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
VB.NET怎样设计定时关机???
小弟今天想设计一个定时关机!!!!!
各位大哥帮帮忙,帮小弟这个忙啊!!!!!
__________________________________________________________________________
做一个时使用时钟控件,与当前的时间进行比较,在自己设定的条件下调用关机函数
__________________________________________________________________________
网上相关的文章很多。
会调用API,会使用Timer控件,基本上就可以搞定了。
__________________________________________________________________________
http://blog.csdn.net/ChengKing/archive/2005/12/24/561259.aspx
__________________________________________________________________________
想要简单的关机代码!!
__________________________________________________________________________
==,给你找找
__________________________________________________________________________
过程名称:LogOutUsers,CloseComputer,ResetComputer
功能描述:注销用户,关闭计算机,重新启动
接收参数:无
返回参数:无
创建人员及日期:zzz@2007-02-01
注意事项:此函数会对当前的系统有效
============================================================================================================
Private Declare Function ExitWindowsEx Lib user32 (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Const EWX_FORCE As Integer = 4
Const EWX_LOGOFF As Integer = 0
Const EWX_REBOOT As Integer = 2
Const EWX_SHUTDOWN As Integer = 1

注销当前用户
Public Sub LogOutUsers()
ExitWindowsEx(EWX_FORCE, 0)
End Sub

关闭机算机
Public Sub CloseComputer()
ExitWindowsEx(EWX_SHUTDOWN, 0)
End Sub

重新启动
Public Sub ResetComputer()
ExitWindowsEx(EWX_REBOOT, 0)
End Sub
__________________________________________________________________________
试试这个,我是把这个放在全局模块里用的
__________________________________________________________________________
hehe ...学习.
__________________________________________________________________________
上面的代码关机和重启动都不行啊!!!
只是注销的那个才行啊!!!
能不能再帮帮我???
__________________________________________________________________________
直接调用系统程序,我自己写了个定时关机的,仅限winxp
要的话发邮件lllllooooo@qq.com
__________________________________________________________________________
Shell( shutdown -s -t 30 )

三十秒关机 你时宜式

没办法 我关机了
__________________________________________________________________________
关机类

Imports System
Imports System.Text
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class ExitWindows

Public Enum RestartOptions
LogOff = 0
PowerOff = 8
Reboot = 2
ShutDown = 1
Suspend = -1
Hibernate = -2
EWX_FORCE = 4
End Enum

Public Structure LUID
Dim LowPart As Integer
Dim HighPart As Integer
End Structure

Public Structure LUID_AND_ATTRIBUTES
Dim pLuid As LUID
Dim Attributes As Integer
End Structure

Public Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Integer
Dim Privileges As LUID_AND_ATTRIBUTES
End Structure

Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const EWX_FORCE = 4

Private Declare Function LoadLibrary Lib kernel32 Alias LoadLibraryA (ByVal lpLibFileName As String) As IntPtr
Private Declare Function FreeLibrary Lib kernel32 (ByVal hLibModule As IntPtr) As Integer
Private Declare Function GetProcAddress Lib kernel32 (ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
Private Declare Function SetSuspendState Lib Powrprof (ByVal Hibernate As Integer, ByVal ForceCritical As Integer, ByVal DisableWakeEvent As Integer) As Integer
Private Declare Function OpenProcessToken Lib advapi32.dll (ByVal ProcessHandle As IntPtr, ByVal DesiredAccess As Integer, ByRef TokenHandle As IntPtr) As Integer
Private Declare Function LookupPrivilegeValue Lib advapi32.dll Alias LookupPrivilegeValueA (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer
Private Declare Function AdjustTokenPrivileges Lib advapi32.dll (ByVal TokenHandle As IntPtr, ByVal DisableAllPrivileges As Integer, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer
Private Declare Function ExitWindowsEx Lib user32 (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Private Declare Function FormatMessage Lib kernel32 Alias FormatMessageA (ByVal dwFlags As Integer, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer

Private Sub ExitWindows(ByVal how As RestartOptions, ByVal force As Boolean)
Select Case how
Case RestartOptions.Suspend
SuspendSystem(False, force)
Case RestartOptions.Hibernate
SuspendSystem(True, force)
Case RestartOptions.ShutDown
how += RestartOptions.PowerOff
ExitWindows(Convert.ToInt32(how), force)
Case Else
ExitWindows(Convert.ToInt32(how), force)
End Select
End Sub

Private Sub ExitWindows(ByVal how As Integer, ByVal force As Boolean)
EnableToken( SeShutdownPrivilege )
If force Then how = how Or EWX_FORCE
If (ExitWindowsEx(how, 0) = 0) Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
End Sub

Private Sub EnableToken(ByVal privilege As String)
If Not CheckEntryPoint( advapi32.dll , AdjustTokenPrivileges ) Then Return
Dim tokenHandle As IntPtr = IntPtr.Zero
Dim privilegeLUID = New LUID
Dim newPrivileges = New TOKEN_PRIVILEGES
Dim tokenPrivileges As TOKEN_PRIVILEGES
If (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, tokenHandle)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
If (LookupPrivilegeValue( , privilege, privilegeLUID)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
tokenPrivileges.PrivilegeCount = 1
tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED
tokenPrivileges.Privileges.pLuid = privilegeLUID
Dim Size As Integer = 4
If (AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), newPrivileges, Size)) = 0 Then Throw New PrivilegeException(FormatError(Marshal.GetLastWin32Error()))
End Sub

Private Sub SuspendSystem(ByVal hibernate As Boolean, ByVal force As Boolean)
If Not CheckEntryPoint( powrprof.dll , SetSuspendState ) Then Throw New PlatformNotSupportedException( The SetSuspendState method is not supported on this system! )
SetSuspendState(Convert.ToInt32(IIf(hibernate, 1, 0)), Convert.ToInt32(IIf(force, 1, 0)), 0)
End Sub

Private Function CheckEntryPoint(ByVal library As String, ByVal method As String) As Boolean
Dim libPtr As IntPtr = LoadLibrary(library)
If Not libPtr.Equals(IntPtr.Zero) Then
If Not GetProcAddress(libPtr, method).Equals(IntPtr.Zero) Then
FreeLibrary(libPtr)
Return True
End If
FreeLibrary(libPtr)
End If
Return False
End Function

Private Function FormatError(ByVal number As Integer) As String
Dim Buffer = New StringBuilder(255)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, Buffer, Buffer.Capacity, 0)
Return Buffer.ToString()
End Function

Public Sub LogOff() 注销
ExitWindows(RestartOptions.LogOff, False)
End Sub

Public Sub Reboot() 重启计算机
ExitWindows(RestartOptions.Reboot, False)
End Sub

Public Sub ShutDown() 关闭系统
ExitWindows(RestartOptions.ShutDown, False)
End Sub

Public Sub ShutDownForce() 强制关闭系统
ExitWindows(RestartOptions.ShutDown, True)
End Sub

Public Sub Suspend() 待机
ExitWindows(RestartOptions.Suspend, False)
End Sub

Public Sub Hibernate() 休眠
ExitWindows(RestartOptions.Hibernate, False)
End Sub

End Class

Public Class PrivilegeException
Inherits Exception

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
__________________________________________________________________________
支持!学习ing~~
__________________________________________________________________________
用系统自带的shutdown.exe最省事,xp以上都好用。
可以看shutdown帮助。
__________________________________________________________________________
注销
Shell( Shutdown.exe /l /t 0 )
重启
Shell( Shutdown.exe /r /f /t 0 )
关机
Shell( shutdown.exe /s /f /t 0 )

使用Shutdown命令
system.Diagnostics.Process.Start( shutdown.exe )

关于ShutDown的参数描述如下:
===============================================================
shutdown [/i | /l | /s | /r | /a | /p | /h | /e] [/f]
[/m //computer][/t xxx][/d [p:]xx:yy [/c comment ]]

/i 显示图形用户界面(GUI)。
这必须是第一个选项
/l 注销。这不能与 /m 或 /d 选项一起使用
/s 关闭计算机
/r 关闭并重启动计算机
/a 放弃系统关闭。
这只能在超时过程中使用
/p 关闭本地计算机,没有超时或警告。
这只能与 /d 选项一起使用
/h 休眠本地计算机。
这只能与 /f 选项一起使用
/e 将计算机的意外关闭原因记入文档
/m //computer 指定目标计算机
/t xxx 设置关闭前的超时为 xxx 秒。
有效范围是 0-600,默认为 30
/c comment 重启动或关闭的原因的注释。
最大允许 127 个字符
/f 强制正在运行的应用程序关闭而不事先警告用户
/d [p:]xx:yy 提供重启动或关闭的原因
p 表明重启动或关闭是计划内的
xx 是主要原因号(小于 256 的正整数)
yy 是次要原因号(小于 65536 的正整数)
原因:
(E = 预期 U = 意外 P = 计划, C = 自定义)
========================================================

例:关机
system.Diagnostics.Process.Start( shutdown.exe , s )
重新启动
system.Diagnostics.Process.Start( shutdown.exe , r )
注销
system.Diagnostics.Process.Start( shutdown.exe , l )

最后在此操作之后 马上退出当前应用程序:
system.Diagnostics.Process.Start( shutdown.exe , s )
Application.Exit()
__________________________________________________________________________
顶一下
__________________________________________________________________________
我设置了关机,但怎样才能取消关机啊!!!
__________________________________________________________________________
  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值