Czy zdarza się
wam czasem potrzeba wyłączania/hibernowania/usypiania komputera za
jakiś czas, np. po wykonaniu jakiegoś długotrwałego zadania? Mnie
tak. Jak więc to zrobić? Niektóre aplikacje taką funkcjonalność
posiadają, jednak nie wszystkie. Z pomocą może przyjść systemowe
polecenie shutdown, ale nie potrafi ono wszystkiego.
„Shutdown /s”
- wyłączy system, „Shutdown /h” - system zahibernuje, można
dodać jeszcze opóźnienie czasowe np. „Shutdown /s /t 600”, by
wyłączyć system po 10 min (600 s). Niestety opóźnienie działa
tylko wraz z opcją wyłączania (/s), autorzy nie pomyśleli, że
ktoś będzie chciał np. zahibernować komputer po 10 min.
Cóż z tym
zrobić? Na pomysł wpadłem jakiś czas temu, gdy na 9fingers
znalazłem pytanie: Zamykanie,
restart systemu - program w c# . Znałem odpowiedź, więc ją
podałem:
//źródło: http://msdn.microsoft.com/en-us/library/aa394058(VS.85).aspx
// Win32Shutdown Method of the Win32_OperatingSystem Class
// The Win32Shutdown WMI class method provides the full set of shutdown options supported by Win32 operating systems. These include logoff, shutdown, reboot, and forcing a logoff, shutdown, or reboot. The calling process must have the SE_SHUTDOWN_NAME privilege.
// Windows NT 4.0 and Windows Me/98/95: SE_SHUTDOWN_NAME privilege is not required.
//This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
//Syntax:
//uint32 Win32Shutdown(
// sint32 Flags,
// sint32 Reserved
//);
//Parameters:
//- Flags:
// Bitmapped set of flags to shut the computer down. To force a command, add the Force flag (4) to the command value. Using Force in conjunction with Shutdown or Reboot on a remote computer immediately shuts down everything (including WMI, COM, and so on), or reboots the remote computer. This results in an indeterminate return value.
// Value Meaning
// 0 0x0 Log Off
// 4 0x4 Forced Log Off (0 + 4)
// 1 0x1 Shutdown
// 5 0x5 Forced Shutdown (1 + 4)
// 2 0x2 Reboot
// 6 0x6 Forced Reboot (2 + 4)
// 8 0x8 Power Off
// 12 0xC Forced Power Off (8 + 4)
//- Reserved
// A means to extend Win32Shutdown. Currently, the Reserved parameter is ignored.
public System.UInt32 ForceReboot()
{
System.Management.ManagementBaseObject inParams = PrivateLateBoundObject.GetMethodParameters("Win32Shutdown");
inParams[ "Flags" ] = "6"; // System forced reboot
inParams[ "Reserved" ] = "0";
bool EnablePrivileges = PrivateLateBoundObject.Scope.Options.EnablePrivileges;
PrivateLateBoundObject.Scope.Options.EnablePrivileges = true;
System.Management.ManagementBaseObject outParams = PrivateLateBoundObject.InvokeMethod( "Win32Shutdown", inParams, null );
PrivateLateBoundObject.Scope.Options.EnablePrivileges = EnablePrivileges;
return System.Convert.ToUInt32( outParams.Properties[ "ReturnValue" ].Value );
}
}
Powyżej napisany jest kod wymuszonego restartu, zamknięcie systemu
podobnie tylko inne parametry (flagi) ;).Zaświtało mi jednak: „A może by tak hibernować lub usypiać komputer z poziomu kodu w C#?”. Okazało się to jeszcze prostsze, należy wykorzystać obiekt Application dla aplikacji typu WinForms.
Zobaczcie:
- Hibernacja:
Application.SetSuspendState( PowerState.Hibernate, true, true );
- Usypianie:
Application.SetSuspendState(PowerState.Suspend true, true);
Od teraz napisanie
programiku, który usypia komputer po upływie pewnego czasu, to już
błahostka ;).









Maciej_Zbrzezny on delicious.com
Brak komentarzy:
Prześlij komentarz