Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Which is the best way to shutdown, reboot, or logoff from the system in C# (.NEt 4.0)?

Which is the best way to shutdown, reboot, or logoff from the system in C# (.NEt 4.0)?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erik
    wrote on last edited by
    #1

    Hi, I have to write an application that has the ability to perform a system-restart, shutdown or user-logoff. So far, I have found two solutions to do it, which both work very well. I'd like to know which is the best way to do it, i.e. if there are any disadvantages to the one or the other way to do it. Which is the most professional way to deal with it? Currently, I use the System.Management Namespace, whic looks like the most elegant way to do this to me:

    ManagementBaseObject mboShutdown = null;
    using (ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem"))
    {
    mcWin32.Get();
    mcWin32.Scope.Options.EnablePrivileges = true;
    ManagementBaseObject ShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
    ShutdownParams["Flags"] = "1";
    ShutdownParams["Reserved"] = "0";
    foreach (ManagementObject manObj in mcWin32.GetInstances())
    {
    mboShutdown = manObj.InvokeMethod("Win32Shutdown", ShutdownParams, null);
    }
    ret = true;
    }

    On the other hand, I have found many users who load the appropriate system-DLLs, and perform the whole tsak "unmanaged", an call ExitWindowsEx from these DLLs. This works too, but I am wondering why people decide to do it this way, if there is a namespace available in .NET that actually does it, like in my example above. Which way would you chose, and why?

    L R D 3 Replies Last reply
    0
    • E Erik

      Hi, I have to write an application that has the ability to perform a system-restart, shutdown or user-logoff. So far, I have found two solutions to do it, which both work very well. I'd like to know which is the best way to do it, i.e. if there are any disadvantages to the one or the other way to do it. Which is the most professional way to deal with it? Currently, I use the System.Management Namespace, whic looks like the most elegant way to do this to me:

      ManagementBaseObject mboShutdown = null;
      using (ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem"))
      {
      mcWin32.Get();
      mcWin32.Scope.Options.EnablePrivileges = true;
      ManagementBaseObject ShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
      ShutdownParams["Flags"] = "1";
      ShutdownParams["Reserved"] = "0";
      foreach (ManagementObject manObj in mcWin32.GetInstances())
      {
      mboShutdown = manObj.InvokeMethod("Win32Shutdown", ShutdownParams, null);
      }
      ret = true;
      }

      On the other hand, I have found many users who load the appropriate system-DLLs, and perform the whole tsak "unmanaged", an call ExitWindowsEx from these DLLs. This works too, but I am wondering why people decide to do it this way, if there is a namespace available in .NET that actually does it, like in my example above. Which way would you chose, and why?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I have used neither one. I think I once used something akin to "Process.Start("shutdown"). :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      1 Reply Last reply
      0
      • E Erik

        Hi, I have to write an application that has the ability to perform a system-restart, shutdown or user-logoff. So far, I have found two solutions to do it, which both work very well. I'd like to know which is the best way to do it, i.e. if there are any disadvantages to the one or the other way to do it. Which is the most professional way to deal with it? Currently, I use the System.Management Namespace, whic looks like the most elegant way to do this to me:

        ManagementBaseObject mboShutdown = null;
        using (ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem"))
        {
        mcWin32.Get();
        mcWin32.Scope.Options.EnablePrivileges = true;
        ManagementBaseObject ShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
        ShutdownParams["Flags"] = "1";
        ShutdownParams["Reserved"] = "0";
        foreach (ManagementObject manObj in mcWin32.GetInstances())
        {
        mboShutdown = manObj.InvokeMethod("Win32Shutdown", ShutdownParams, null);
        }
        ret = true;
        }

        On the other hand, I have found many users who load the appropriate system-DLLs, and perform the whole tsak "unmanaged", an call ExitWindowsEx from these DLLs. This works too, but I am wondering why people decide to do it this way, if there is a namespace available in .NET that actually does it, like in my example above. Which way would you chose, and why?

        R Offline
        R Offline
        Ravi Sant
        wrote on last edited by
        #3

        The un-managed code is not recommended one in long term, although it is faster way to achieve same. With managed, good way is:

        using System.Diagnostics;

        ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r");

        Process.Start(startinfo);

        Keep in mind the options you want to use: -i Display GUI interface, must be the first option -l Log off (cannot be used with -m option) -s Shutdown the computer -r Shutdown and restart the computer -a Abort a system shutdown -m \\computername Remote computer to shutdown/restart/abort -t xx Set timeout for shutdown to xx seconds -c "comment" Shutdown comment (maximum of 127 characters) -f Forces running applications to close without warning -d [u][p]:xx:yy The reason code for the shutdown, u is the user code, p is a planned shutdown code, xx is the major reason code (positive integer less than 256) ,yy is the minor reason code (positive integer less than 65536)

        D 1 Reply Last reply
        0
        • R Ravi Sant

          The un-managed code is not recommended one in long term, although it is faster way to achieve same. With managed, good way is:

          using System.Diagnostics;

          ProcessStartInfo startinfo = new ProcessStartInfo("shutdown.exe","-r");

          Process.Start(startinfo);

          Keep in mind the options you want to use: -i Display GUI interface, must be the first option -l Log off (cannot be used with -m option) -s Shutdown the computer -r Shutdown and restart the computer -a Abort a system shutdown -m \\computername Remote computer to shutdown/restart/abort -t xx Set timeout for shutdown to xx seconds -c "comment" Shutdown comment (maximum of 127 characters) -f Forces running applications to close without warning -d [u][p]:xx:yy The reason code for the shutdown, u is the user code, p is a planned shutdown code, xx is the major reason code (positive integer less than 256) ,yy is the minor reason code (positive integer less than 65536)

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          RaviSant wrote:

          The un-managed code is not recommended one in long term

          Got a citation for this??

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          J 1 Reply Last reply
          0
          • E Erik

            Hi, I have to write an application that has the ability to perform a system-restart, shutdown or user-logoff. So far, I have found two solutions to do it, which both work very well. I'd like to know which is the best way to do it, i.e. if there are any disadvantages to the one or the other way to do it. Which is the most professional way to deal with it? Currently, I use the System.Management Namespace, whic looks like the most elegant way to do this to me:

            ManagementBaseObject mboShutdown = null;
            using (ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem"))
            {
            mcWin32.Get();
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject ShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
            ShutdownParams["Flags"] = "1";
            ShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
            mboShutdown = manObj.InvokeMethod("Win32Shutdown", ShutdownParams, null);
            }
            ret = true;
            }

            On the other hand, I have found many users who load the appropriate system-DLLs, and perform the whole tsak "unmanaged", an call ExitWindowsEx from these DLLs. This works too, but I am wondering why people decide to do it this way, if there is a namespace available in .NET that actually does it, like in my example above. Which way would you chose, and why?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            The most direct and rock solid way to do it, with the greatest amount of control, is ExitWindowsEx. I don't like using WMI because it has a habit of silently commiting suicide and you're left wondering why your call that used to work fine suddenly fails with cryptic errors, like "RPC Server unavailable".

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              RaviSant wrote:

              The un-managed code is not recommended one in long term

              Got a citation for this??

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Dave Kreskowiak wrote:

              Got a citation for this??

              Certainly is something I try to avoid since incorrect usage of interop or even the unmanaged itself code can cause an OS system exception thus exiting the app. That behavior is not acceptable in a server application. Probably not something that one wants to occur in any app as well. The following certainly would suggest that there are reasons that one might want to look for other options. http://msdn.microsoft.com/en-us/library/ms973872.aspx#manunman_topic4[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups