从 WCF 服务不工作关闭或重新启动计算机

Shutdown or Restart Computer from WCF Service Not Working

我在 IIS 7.5 中托管的 WCF 服务中有一个函数,它的任务是在调用时关闭或重新启动计算机。

我使用内置命令行并通过传递 "shutdown.exe -t 0 -r" 重新启动或 "shutdown.exe -t 0 -s" 关闭来执行它。

    Try
        Using process As New System.Diagnostics.Process()
            Dim startInfo As New System.Diagnostics.ProcessStartInfo()
            With startInfo
                .WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
                .CreateNoWindow = True
                .UseShellExecute = False
                .RedirectStandardError = True
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .FileName = "shutdown.exe"
                .Arguments = "-t 0 -r"
            End With
            If System.Environment.OSVersion.Version.Major >= 6 Then process.StartInfo.Verb = "runas"
            process.StartInfo = startInfo
            process.Start()
            process.WaitForExit()
            If process.ExitCode = 0 Then
                Return True
            Else
                Return False
            End If
        End Using
    Catch ex As Exception
        Return False
    End Try

如果手动在命令提示符下执行,命令行工作正常。但是当它在 WCF 服务中执行时它不起作用。

我猜你的服务没有关闭电脑的权限(我猜欠权限的用户是网络用户,不能这样做)。 Check out Event Log for details,我很确定它包含您问题的答案。

查看there如何在特定帐户下运行 wcf 服务。

好的,尝试了所有方法并保持失败,并以 this guy who use bat file 告终。它有效..!

这太奇怪了,仍然不知道为什么会这样。现在...谁在乎:)...等我有空的时候我会想办法弄清楚。

        ....
        Dim strCmd As String = "SHUTDOWN -m {0} -f -t 0 -r"           
        File.WriteAllText(_pathCmd & "cmd_restart.bat", String.Format(strCmd, "127.0.0.1"))
        Return ExecuteCommand(_pathCmd & "cmd_restart.bat", "")