2020-11-17


C#操作 windows系统命令

在国内搜索半天 还是老外厉害啊。

1.

重启(示例):

Try this:

System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0");

2.

64的操作选项:
Syntax :语法
SHUTDOWN [logoff_option] [/m \Computer] [options]:

logoff_option:
    /i         Display the GUI (must be the first option)
    /l         Log off. This cannot be used with /m or /d option
    /s         Shutdown
    /r         Shutdown and Restart
    /a         Abort a system shutdown.
               (only during the time-out period)
    /p         Turn off the local computer with no time-out or warning
               (only with /d)
    /h         Hibernate the local computer (only with /f )
    /e         Document the reason for an unexpected shutdown of a computer

Options:

   /m \\Computer  : A remote computer to shutdown.

   /t:xxx         : Time until system shutdown in seconds. 
                    The valid range is xxx=0-600 seconds. [default=30]
   /c "Msg"       : An optional shutdown message [Max 127 chars]

   /f             : Force running applications to close.
                    This will not prompt for File-Save in any open applications.
                    so will result in a loss of all unsaved data!!!

   /d u:xx:yy     : List a USER reason code for the shutdown. 
   /d P:xx:yy     : List a PLANNED reason code for the shutdown.
                     xx Specifies the major reason code (0-255)
                     yy Specifies the minor reason code (0-65536)

Working code inside a windows service:  

var cmd = new System.Diagnostics.ProcessStartInfo("shutdown.exe", "-r -t 0");
cmd.CreateNoWindow = true;
cmd.UseShellExecute = false;
cmd.ErrorDialog = false;
System.Diagnostics.Process.Start(cmd);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值