IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[C#] Faire reboot le systeme


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    -
    Inscrit en
    Août 2003
    Messages
    80
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : -

    Informations forums :
    Inscription : Août 2003
    Messages : 80
    Par défaut [C#] Faire reboot le systeme
    Salut,


    Je cherche la commande pour faire reboot le systeme, si quelqu'un peux m'aider ?


    Merci

  2. #2
    Membre éclairé Avatar de Davide
    Profil pro
    Inscrit en
    Août 2003
    Messages
    80
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2003
    Messages : 80
    Par défaut
    Je te conseille de chercher du coté des classes WMI..
    http://msdn.microsoft.com/library/fr...tframework.asp

  3. #3
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Sinon, essaye ce (vieux) bout de code que je viens de retrouver :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
     
    [StructLayout(LayoutKind.Sequential)]
    		public struct ProcessEntry32
    		{
    			public uint dwSize;
    			public uint cntUsage;
    			public uint th32ProcessID;
    			public IntPtr th32DefaultHeapID;
    			public uint th32ModuleID;
    			public uint cntThreads;
    			public uint th32ParentProcessID;
    			public int pcPriClassBase;
    			public uint dwFlags;
     
    			[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] 
    			public string szExeFile;
    		};
     
     
    		[DllImport("KERNEL32.DLL")]
    		public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);
     
    		[DllImport("KERNEL32.DLL")]
    		public static extern int Process32First(int handle, ref ProcessEntry32 pe);
     
    		[DllImport("KERNEL32.DLL")]
    		public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
     
    		[DllImport("KERNEL32.DLL")]
    		public static extern int CloseHandle(int handle);
     
    		public uint SNAPPROCESS = 2;
     
    		[DllImport("KERNEL32.DLL")]
    		public static extern int OpenProcess(uint dwDesiredAccess, int bInheritHandle, uint dwProcessId);
     
    		[StructLayout(LayoutKind.Sequential)]
    		public class PROCESS_MEMORY_COUNTERS
    		{
    			public int cb;
    			public int PageFaultCount;
    			public int PeakWorkingSetSize;
    			public int WorkingSetSize;
    			public int QuotaPeakPagedPoolUsage;
    			public int QuotaPagedPoolUsage;
    			public int QuotaPeakNonPagedPoolUsage;
    			public int QuotaNonPagedPoolUsage;
    			public int PagefileUsage;
    			public int PeakPagefileUsage;
    		}
     
    		[DllImport("psapi.dll")]
    		public static extern int GetProcessMemoryInfo(int hProcess, [Out] PROCESS_MEMORY_COUNTERS counters, int size);
     
    		[StructLayout(LayoutKind.Sequential)]
    		public struct LUID
    		{
    			public uint LowPart;
    			public uint HighPart;
    		};
     
    		public const int ANYSIZE_ARRAY = 1;
     
    		[StructLayout(LayoutKind.Sequential)]
    		public struct TOKEN_PRIVILEGES
    		{
    			public uint PriviledgeCount;
    			public LUID Luid;
    			public uint Attributes;		
    		}
     
    		[DllImport("KERNEL32.DLL")]
    		public static extern int GetCurrentProcess();
     
    		[DllImport("advapi32.dll")]
    		public static extern System.Boolean OpenProcessToken(
    			int ProcessHandle,
    			uint DesiredAccess,
    			ref int Tokenhandle
    		);
     
    		[DllImport("advapi32.dll")]
    		public static extern System.Boolean LookupPrivilegeValue(
    			string lpSsytemName,
    			string lpName,
    			ref LUID pluid
    		);
     
    		[DllImport("advapi32.dll")]
    		public static extern int AdjustTokenPrivileges(
    			int TokenHandle,
    			int DisableAllPrivileges,
    			[MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState,
    			int BufferLength,
    			int PreviousState,
    			int ReturnLength
    		);
     
    		[DllImport("user32.dll")]
    		public static extern bool ExitWindowsEx(int flg, int rea);
     
    		public enum EWX_ENUM
    		{
    			EWX_LOGOFF = 0x00000000,
    			EWX_SHUTDOWN = 0x00000001,
    			EWX_REBOOT = 0x00000002,
    			EWX_FORCE = 0x00000004,
    			EWX_POWEROFF= 0x00000008,
    			EWX_FORCEIFHUNG = 0x00000010,
    			EWX_FORCEREBOOT = EWX_REBOOT | EWX_FORCE,
    			EWX_FORCEIFHUNGREBOOT = EWX_REBOOT | EWX_FORCEIFHUNG,
    			EWX_FORCESHUTDOWN = EWX_SHUTDOWN | EWX_FORCE,
    			EWX_FORCEIFHUNGSHUTDOWN = EWX_SHUTDOWN | EWX_FORCEIFHUNG,
    			EWX_FORCEPOWEROFF = EWX_POWEROFF | EWX_FORCE,
    			EWX_FORCEIFHUNGPOWEROFF = EWX_POWEROFF | EWX_FORCEIFHUNG,
    			EWX_FORCELOGOFF = EWX_LOGOFF | EWX_FORCE,
    			EWX_FORCEIFHUNGLOGOFF = EWX_LOGOFF | EWX_FORCEIFHUNG
    		}
     
    		public static bool FindProcessByName(string pname)
    		{
    			int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
    			ProcessEntry32 pe32 = new ProcessEntry32();
    			pe32.dwSize = 296;
     
    			while(Process32Next(SnapShot, ref pe32) != 0)
    			{
     
    				string xname = pe32.szExeFile.ToString();
     
    				if(pname.CompareTo(xname) == 0)
    				{
    					CloseHandle(SnapShot);
    					return true;
    				}
    			}
    			CloseHandle(SnapShot);
    			return false;
    		}
     
    		public static int GetNumberRunning(string pname)
    		{
    			int ProcCount = 0;
    			int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
    			ProcessEntry32 pe32 = new ProcessEntry32();
    			pe32.dwSize = 296;
    			while(Process32Next(SnapShot, ref pe32) != 0)
    			{
    				string xname = pe32.szExeFile.ToString();
    				if(pname.CompareTo(xname) == 0)
    				{
    					ProcCount++;
    				}
    			}
    			CloseHandle(SnapShot);
    			return ProcCount;
    		}
     
    		public static uint GetProcessIdForProcess(string pname)
    		{
    			uint procid = 0;
    			int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
    			ProcessEntry32 pe32 = new ProcessEntry32();
    			pe32.dwSize = 296;
    			while(Process32Next(SnapShot, ref pe32) != 0)
    			{
    				string xname = pe32.szExeFile.ToString();
    				if(pname.CompareTo(xname) == 0)
    				{
    					procid = pe32.th32ProcessID;
    					break;
    				}
    			}
    			CloseHandle(SnapShot);
    			return procid;
    		}
     
    		public static int GetMemoryUsageForProcess(string pname)
    		{
    			try
    			{
    				return GetMemoryUsageForProcess(GetProcessIdForProcess(pname));
    			}
    			catch
    			{
    				return -1;
    			}
    		}
     
    		public static int GetMemoryUsageForProcess(uint pid)
    		{
    			int mem = 0;
    			int pHandle = OpenProcess(0x0400 | 0x0010, 0, pid);
    			PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
    			if(GetProcessMemoryInfo(pHandle, pmc, 40) != 0)
    			{
    				mem = pmc.WorkingSetSize;
    			}
    			CloseHandle(pHandle);
    			return mem;
    		}
     
    		public static bool IsMoreThanOneRunning(string pname)
    		{
    			if (GetNumberRunning(pname) > 1)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
    		}		
     
     
    		public static void ExitSystem(int EWX_VALUE)
    		{
    			TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
    			TOKEN_PRIVILEGES tpOld = new TOKEN_PRIVILEGES();
    			LUID luid = new LUID();
     
    			LookupPrivilegeValue(null, "SeShutdownPrivilege",ref luid);
    			int processHandle = GetCurrentProcess();
    			int TokenHandle = 0;
    			OpenProcessToken(processHandle, 0x00000020 | 0x00000008, ref TokenHandle);
     
    			tp.PriviledgeCount = 1;
    			tp.Attributes = 0x00000002;
    			tp.Luid = luid;
     
    			int tpsz = Marshal.SizeOf(tp);
    			tpsz = AdjustTokenPrivileges(TokenHandle, 0, ref tp, tpsz,0,0);
    			ExitWindowsEx(EWX_VALUE, 0);
    		}

    Et pour lancer le reboot:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ExitSystem(Convert.ToInt32(EWX_ENUM.EWX_FORCEREBOOT));
    A+

  4. #4
    Membre éclairé Avatar de Davide
    Profil pro
    Inscrit en
    Août 2003
    Messages
    80
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2003
    Messages : 80
    Par défaut
    Désolé j'ai pas trouvé sur le MSDN french..
    http://msdn.microsoft.com/library/de...tingsystem.asp

    Sinon la méthode de morpheus est toute prête..

  5. #5
    Membre confirmé
    Homme Profil pro
    -
    Inscrit en
    Août 2003
    Messages
    80
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : -

    Informations forums :
    Inscription : Août 2003
    Messages : 80
    Par défaut
    Merci a tout les deux.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Faire disparaitre un System.out.println
    Par identifiant_bidon dans le forum Langage
    Réponses: 6
    Dernier message: 28/09/2010, 12h00
  2. Comment faire fonctionner Remote System Explorer ?
    Par coolben dans le forum Eclipse PHP
    Réponses: 0
    Dernier message: 12/03/2010, 13h50
  3. Réponses: 9
    Dernier message: 03/10/2007, 21h45
  4. Réponses: 13
    Dernier message: 22/07/2005, 15h24
  5. Est il possible de faire planter un système Unix
    Par Patrick PETIT dans le forum Administration système
    Réponses: 15
    Dernier message: 15/06/2004, 15h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo