Shut Down PC

Is there a way to shut down a PC using the framenetwork

I tried the "shutdown /s" command but the shell doesn't work on my PC.

Thanks.



Answer this question

Shut Down PC

  • wenxincao

    it should see it. Tried specifying shutdown.exe at least its working now ;-) you should also use Process class instead of Shell for many reasons, since its a .NET Class (Process) and is the better way of doing things than using the old VB legacy objects

  • Mnd1

    That's what is probably is, .net won'd detect it unless you use .net statements.  Even is you use shutdown.exe it doesn't find the file.
  • YoungEngineer

    there isnt a class that does this in the .NET Framework but using the shutdown command will work, if your OS supports the shutdown command (Windows 2000, Windows Server 2003, XP and Vista). The correct way of doing so if you have the above:

     

    Dim thePSI as new System.Diagnostics.ProcessStartInfo("shutdown.exe")

    thePSI.Arguments = "/m \\computerName /s /t 10"

    System.Diagnostics.Process.Start(thePSI)

     

    does this not work for you If it works, it should shutdown the computer after 10 seconds

     

    alternatively you could try this method but wouldnt recommend it (permission issues perhaps but also you are P/Invoking which can be expensive sometimes)

     

    imports System.Runtime.InteropServices

    ..

    ..

    Enum ExitWindows

       LogOff = &H0

       ShutDown = &H1

       Reboot = &H2

       PowerOff = &H8

       RestartApps = &H40

       Force = &H4

       ForceIfHung = &H10

    End Enum

     

    Enum ShutdownReason

    MajorApplication = &H40000

    MajorHardware = &H10000

    MajorLegacyApi = &H70000

    MajorOperatingSystem = &H20000

    MajorOther = &H0

    MajorPower = &H60000

    MajorSoftware = &H30000

    MajorSystem = &H50000

    MinorBlueScreen = &HF

    MinorCordUnplugged = &HB

    MinorDisk = &H7

    MinorEnvironment = &HC

    MinorHardwareDriver = &HD

    MinorHotfix = &H11

    MinorHung = &H5

    MinorInstallation = &H2

    MinorMaintenance = &H1

    MinorMMC = &H19

    MinorNetworkConnectivity = &H14

    MinorNetworkCard = &H9

    MinorOther = &H0

    MinorOtherDriver = &HE

    MinorPowerSupply = &HA

    MinorProcessor = &H8

    MinorReconfig = &H4

    MinorSecurity = &H13

    MinorSecurityFix = &H12

    MinorSecurityFixUninstall = &H18

    MinorServicePack = &H10

    MinorServicePackUninstall = &H16

    MinorTermSrv = &H20

    MinorUnstable = &H6

    MinorUpgrade = &H3

    MinorWMI = &H15

    FlagUserDefined = &H40000000

    FlagPlanned = &H80000000

    End Enum

    ...

    ...

     

    <DllImport("user32.dll", SetLastError:=true)>Private Shared Function ExitWindowsEx(byval uFlags as ExitWindows, byval dwFlags as ShutdownReason) as Boolean

    End Function

     

    'calling it:

     

    ExitWindowsEx(ExitWindows.ShutDown, ShutdownReason.MajorOther And ShutdowReason.MinorOther) 'not sure if this works well, experiment!

     



  • CodeDigger

    It works at last but I'm curious how

    Process.Start("shutdown /s")

    doesn't work.  It says it can't find the file and this doesn't work either...

    Shell "shutdown /s"

    process.start(SystemDirectory & "\shutdown /s")   - can't find file

    However,  Start| Run| Cmd |shutdown /s    does work... so the OS is able to

    find shutdown from any directory, but with shell or process can't.

     


  • Shut Down PC