您的位置:程序门 -> .net技术 -> vb.net



vb.net怎样设计定时关机???


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb.net怎样设计定时关机???
发表于:2007-04-07 01:54:47 楼主
小弟今天想设计一个定时关机!!!!!
各位大哥帮帮忙,帮小弟这个忙啊!!!!!
发表于:2007-04-07 08:39:491楼 得分:0
做一个时使用时钟控件,与当前的时间进行比较,在自己设定的条件下调用关机函数
发表于:2007-04-07 08:43:572楼 得分:0
网上相关的文章很多。
会调用api,会使用timer控件,基本上就可以搞定了。
发表于:2007-04-07 09:18:083楼 得分:0
http://blog.csdn.net/chengking/archive/2005/12/24/561259.aspx
发表于:2007-04-07 14:55:004楼 得分:0
想要简单的关机代码!!
发表于:2007-04-07 14:58:095楼 得分:0
==,给你找找
发表于:2007-04-07 14:58:476楼 得分:0
'过程名称: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
发表于:2007-04-07 14:59:307楼 得分:0
试试这个,我是把这个放在全局模块里用的
发表于:2007-04-07 15:29:268楼 得分:0
hehe   ...学习.
发表于:2007-04-07 16:56:209楼 得分:0
上面的代码关机和重启动都不行啊!!!
只是注销的那个才行啊!!!
能不能再帮帮我???
发表于:2007-04-07 20:21:5610楼 得分:0
直接调用系统程序,我自己写了个定时关机的,仅限winxp
要的话发邮件lllllooooo@qq.com
发表于:2007-04-07 20:45:2511楼 得分:0
shell( "shutdown   -s   -t   30 ")

三十秒关机   你时宜式

没办法   我关机了
发表于:2007-04-07 22:25:5112楼 得分:0
关机类

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
发表于:2007-04-07 22:42:0613楼 得分:0
支持!学习ing~~
发表于:2007-04-08 01:13:5314楼 得分:0
用系统自带的shutdown.exe最省事,xp以上都好用。
可以看shutdown帮助。
发表于:2007-04-08 07:57:3515楼 得分:0
'注销
                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()
发表于:2007-04-08 08:10:1916楼 得分:0
顶一下
发表于:2007-04-08 12:21:3617楼 得分:0
我设置了关机,但怎样才能取消关机啊!!!


快速检索

最新资讯
热门点击