Comment trouver le chemin Windows Service exe

j'ai un service windows et j'ai besoin de créer un répertoire pour stocker des infos. Le chemin doit être relatif au service windows fichier exe. Comment obtenir ce chemin de fichier exe ?

47
demandé sur Incognito 2010-05-14 16:09:29

9 réponses

97
répondu Incognito 2010-05-14 12:10:29

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

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ + ServiceName

Il ya des clés à propos de windows service

33
répondu TC Tarım Köy İşleri 2013-09-03 12:36:21

au lieu d'utiliser un répertoire relatif à l'exécutable, et donc nécessitant des privilèges d'administrateur, pourquoi ne pas utiliser le répertoire commun de données d'application, qui est accessible via

Environment.GetFolderPath(SpecialFolder.CommonApplicationData)

de cette façon, votre application n'a pas besoin d'accéder en écriture à son propre répertoire d'installation, ce qui vous rend plus sûr.

12
répondu Stewart 2010-05-14 12:13:17

pour obtenir le chemin pour le service, vous pouvez utiliser L'objet de gestion. réf: https://msdn.microsoft.com/en-us/library/system.management.managementobject (v=V110).aspx 151930920" http://dotnetstep.blogspot.com/2009/06/get-windowservice-executable-path-in.html

using System.Management;
string ServiceName = "YourServiceName";
using (ManagementObject wmiService = new ManagementObject("Win32_Service.Name='"+ ServiceName +"'"))
                {
                    wmiService.Get();
                    string currentserviceExePath = wmiService["PathName"].ToString();
                    Console.WriteLine(wmiService["PathName"].ToString());
                }
12
répondu Abhay 2016-08-21 21:08:51

Essayez cette

System.Reflection.Assembly.GetEntryAssembly().Location
8
répondu ramin eftekhari 2013-05-27 05:59:16
string exe = Process.GetCurrentProcess().MainModule.FileName;
string path = Path.GetDirectoryName(exe); 

svchost.exe est l'exécutable qui exécute votre service qui est dans system32. Nous devons donc nous rendre au module qui est exécuté par le processus.

8
répondu Sachin 2015-02-04 21:01:52

le répertoire par défaut pour un service windows est le répertoire System 32. Dans votre service, cependant, vous pouvez changer le répertoire courant vers le répertoire que vous avez spécifié dans l'installation du service en faisant ce qui 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.Substring(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);
4
répondu derek 2010-05-14 12:19:15

Cela a fonctionné pour moi

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);    
0
répondu Danw25 2018-04-25 08:16:55

si vous voulez obtenir l'accès du dossier de fichiers de programme ou tout autre en utilisant la programmation, vous devez utiliser le code ci-dessous qui est de fournir des droits pour le dossier spécifique.

 private bool GrantAccess(string 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;
        }
-1
répondu Ebg Test 2018-02-21 12:45:53