Comment trouver le chemin d’exe du service Windows

J’ai un service Windows et j’ai besoin de créer un répertoire pour stocker des informations. Le chemin du répertoire doit être relatif au fichier exe du service Windows. Comment obtenir ce chemin de fichier exe?

Vous pouvez utiliser AppDomain.CurrentDomain.BaseDirectory

Astuce: Si vous voulez trouver le chemin de démarrage du service Windows installé, regardez ici depuis le registre.

 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ + ServiceName 

Il y a des clés sur le service Windows

Au lieu d’utiliser un répertoire relatif à l’exécutable et, par conséquent, nécessitant des privilèges d’administrateur, pourquoi ne pas utiliser le répertoire de données d’application commun, accessible via

 Environment.GetFolderPath(SpecialFolder.CommonApplicationData) 

De cette façon, votre application n’a pas besoin d’un access en écriture à son propre répertoire d’installation, ce qui vous rend plus sûr.

Pour obtenir le chemin du service, vous pouvez utiliser l’object de gestion. réf: https://msdn.microsoft.com/en-us/library/system.management.managementobject(v=vs.110).aspx http://dotnetstep.blogspot.com/2009/06/get-windowservice- executable-path-in.html

 using System.Management; ssortingng ServiceName = "YourServiceName"; using (ManagementObject wmiService = new ManagementObject("Win32_Service.Name='"+ ServiceName +"'")) { wmiService.Get(); ssortingng currentserviceExePath = wmiService["PathName"].ToSsortingng(); Console.WriteLine(wmiService["PathName"].ToSsortingng()); } 

Essaye ça

 System.Reflection.Assembly.GetEntryAssembly().Location 
 ssortingng exe = Process.GetCurrentProcess().MainModule.FileName; ssortingng path = Path.GetDirectoryName(exe); 

svchost.exe est l’exécutable qui exécute votre service qui est dans system32. Par conséquent, nous devons accéder au module qui est exécuté par le processus.

Le répertoire par défaut d’un service Windows est le dossier System32. Dans votre service, cependant, vous pouvez changer le répertoire en cours pour le répertoire que vous avez spécifié dans l’installation du service en procédant comme suit dans votre OnStart:

  // Define working directory (For a service, this is set to System) // This will allow us to reference the app.config if it is in the same directory as the exe Process pc = Process.GetCurrentProcess(); Directory.SetCurrentDirectory(pc.MainModule.FileName.Subssortingng(0, pc.MainModule.FileName.LastIndexOf(@"\"))); 

Edit: une méthode encore plus simple (mais je n’ai pas encore testé):

 System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory); 

Cela a fait le tour pour moi

 Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); 

Si vous voulez avoir access au dossier Program Files ou à tout autre programme en utilisant la programmation, vous devez utiliser le code ci-dessous, qui fournit des droits sur un dossier spécifique.

  private bool GrantAccess(ssortingng fullPath) { DirectoryInfo dInfo = new DirectoryInfo(fullPath); DirectorySecurity dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity); return true; }