Executer un fichier avec des privilèges administrateur
[haut]
Une de vos applications nécessite que vous executiez un fichier/une commande en tant qu'administrateur ?
Télécharger cette source qui vous permet explique comment executer une application, en utilisant un autre compte : l'équivalent du runas en DOS sous Windows 2000/XP.
La source contient également un control réutilisable dans vos applications.
RunAs.cs :
Utiliser RunAs.StartProcess pour démarrer un process
RunAsControl.cs :
Controle contient l'interface graphique pour rentrer les paramètres du process à lancer.
///
/// Creates a new process and its primary thread. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password). It can optionally load the user profile for the specified user.
///
/// This method is untested.
/// This is the name of the user account to log on to. If you use the UPN format, user@domain, the Domain parameter must be NULL. The user account must have the Log On Locally permission on the local computer.
/// Specifies the name of the domain or server whose account database contains the user account. If this parameter is NULL, the user name must be specified in UPN format.
/// Specifies the clear-text password for the user account.
/// Specifies the command line to execute. The maximum length of this string is 32,000 characters. The commandline parameter can be NULL. In that case, the function uses the string pointed to by appname as the command line. If the file name does not contain an extension, .exe is appended. Therefore, if the file name extension is .com, this parameter must include the .com extension. If the file name ends in a period with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file.
/// Logon option. This parameter can be zero or one value from the LogonFlags enum.
/// Specifies the module to execute. The specified module can be a Windows-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer. The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. If the file name does not contain an extension, .exe is assumed. Therefore, if the file name extension is .com, this parameter must include the .com extension. The appname parameter can be NULL. In that case, the module name must be the first white space-delimited token in the commandline string.If the executable module is a 16-bit application, appname should be NULL, and the string pointed to by commandline should specify the executable module as well as its arguments.
/// Use CreationFlags and PriorityFlags enums. Controls how the process is created. Also controls the new process's priority class, which is used to determine the scheduling priorities of the process's threads.
/// Specifies the full path to the current directory for the process. The string can also specify a UNC path. If this parameter is NULL, the new process will have the same current drive and directory as the calling process.
/// Determines whether certain StartUpInfo members are used when the process creates a window. This member can be one or more of the values in the StartUpInfoFlags enum.
/// <Returns a RunAsReturnValue with either the resulting exception or the process. Check HasException to determine if successful.