restart computer using c# code

Hi guys. Could you please explain me how to restart the computer using c#...
What I found on the internet was only for XP....but what about the other OS
I know that for xp it is
using Microsoft.Win32;
and then
System.Diagnostics.Process.Start("ShutDown","/r");
But what about the other OS
Also I have one more question.
Is there a way to retrieve the OS of the user...so that depending on the OS to call different function for restarting the PC...well that's it
Hope you can answer me...

KC.out!


Answer this question

restart computer using c# code

  • CV.

    try this

    Cheers!

  • Whoisit

    I think you can try to call the API "ExitWindowEx"
  • dvdribeiro

    and how do i use this
  • zoffdino

    So this is going to work on every OS the program is running on...I mean like WinXP, WinNT, ME :)
  • maulikk

    i think on win98 wont work..


  • Jonas.S

    Hi i tried to use this above code to retart but this giving me the exception and couil not load my exe form Installer.FormGenerationUpgrade
    any help appricited

    error
    Could not load type 'Installer.FormGenerationUpgrade' from assembly 'GenerationUpgrader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'AdjustTokenPrivileges' has no implementation (no RVA).

  • Marius Onofrei

    Hi,

    What VS are you talking about VS05 won't run on anything lower than Win 2000 with SP4! If there was a change to that, I apologise, and could you please correct me.

    Thanks,

    Eragon.



  • Ashish Pratap

    That was a really nice class...last night(Bulgarian time) I tested it and it works perfectly...thank you man....really appreciate it
  • mihrobert

    what about switch user option what should i add to the function above to have SwitchUser option as well
    I believe that LogOut is teh same as Switch user.



  • Murali T N

    ...

    public enum ShutdownMethod : uint
    {
    LogOff = 0x00,
    ShutDown = 0x01,
    Reboot = 0x02,
    Force = 0x04,
    PowerOff = 0x08,
    ForceIfHung = 0x10
    }

    ....

    what about switch user option what should i add to the function above to have SwitchUser option as well

    thanks in advance.



  • I Mrus

    This is the code that works for me

    [StructLayout(LayoutKind.Sequential, Pack = 1)]

    internal struct TokPriv1Luid

    {

    public int Count;

    public long Luid;

    public int Attr;

    }

    [DllImport("kernel32.dll", ExactSpelling = true)]

    internal static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

    [DllImport("advapi32.dll", SetLastError = true)]

    internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]

    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,

    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]

    internal static extern bool ExitWindowsEx(int flg, int rea);

    internal const int SE_PRIVILEGE_ENABLED = 0x00000002;

    internal const int TOKEN_QUERY = 0x00000008;

    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;

    internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

    internal const int EWX_LOGOFF = 0x00000000;

    internal const int EWX_SHUTDOWN = 0x00000001;

    internal const int EWX_REBOOT = 0x00000002;

    internal const int EWX_FORCE = 0x00000004;

    internal const int EWX_POWEROFF = 0x00000008;

    internal const int EWX_FORCEIFHUNG = 0x00000010;

    public static Thread thread1;

    static void DoExitWin(int flg)

    {

    bool ok;

    TokPriv1Luid tp;

    IntPtr hproc = GetCurrentProcess();

    IntPtr htok = IntPtr.Zero;

    ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);

    tp.Count = 1;

    tp.Luid = 0;

    tp.Attr = SE_PRIVILEGE_ENABLED;

    ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);

    ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);

    ok = ExitWindowsEx(flg, 0);

    }


  • Yifei Wu

    Yes That's Truw that Visual Studio 2005 doesnot work on Windows Version older than 2000 with SP4, but applications developed with it can run on even Windows 98......

    So only for developer its necessary to have atleast Windows 2000 with SP4 or Higher while a user can have Any OS above Windows 98.

    Best Regards,

    Rizwan



  • Mateus1223

    you dont need VS to run your .NET application. you only need the .NET Framework, which .NET Framework 1.1 and 2.0 do support Windows 98, so you can run your .NET 1.1/2.0 app in Windows 98. However you need to find another way of shutting down the machine but also checking which version of the OS you are currently running and executing the right command based on the OS you are on.



  • OTNS

    What will work on Win98 from C# Everything I try either doesn't reboot the computer or causes blue screen explosions.

    Thanks,

    Kevin



  • restart computer using c# code