ProgramingTip

Windows 서비스 exe 경로를 찾는 방법

bestdevel 2020. 12. 31. 23:35
반응형

Windows 서비스 exe 경로를 찾는 방법


Windows 서비스가 필요한 정보를 제공합니다. 디렉토리 경로는 Windows 서비스 exe 파일에 최후해야합니다. 이 exe 파일 경로를 어떻게 얻을 수 있습니까?


AppDomain.CurrentDomain.BaseDirectory 를 사용할 수 있습니다.


팁 : 저렴한 Windows 서비스의 시작 경로를 입고면에서 여기를 찾으세요.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ + ServiceName

Windows 서비스에 대한 키가 있습니다.


서비스 경로를 사용할 수 있습니다. 관리 개체를 사용할 수 있습니다. 참조 : https://msdn.microsoft.com/en-us/library/system.management.managementobject(v=vs.110).aspx http://dotnetstep.blogspot.com/2009/06/get-windowservice- 실행 가능한 경로 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());
                }

실행에 최빈 디렉토리를 사용하여 관리자 권한이 필요한 대신 다음을 통해 액세스 할 수있는 디렉토리 데이터 디렉토리를 사용하지 않는 이유는 무엇입니까?

Environment.GetFolderPath(SpecialFolder.CommonApplicationData)

이렇게하면 앱이 자체 설치 디렉터리에 대한 쓰기 액세스 권한이 필요하지 더 안전합니다.


이 시도

System.Reflection.Assembly.GetEntryAssembly().Location

string exe = Process.GetCurrentProcess().MainModule.FileName;
string path = Path.GetDirectoryName(exe); 

svchost.exe는 system32에있는 서비스를 실행하는 실행 파일입니다. 따라서 프로세스에 의해 실행되는 모듈로 이동해야합니다.


Windows 서비스의 기본 디렉터리는 System32 폴더입니다. 하지만 서비스에서는 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(@"\")));

편집 : 더 간단한 방법 (하지만 아직 테스트하지 않는 것) :

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

이것은 나를 위해 트릭을했다

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

Program Files 폴더 또는 프로그래밍을 사용하여 다른 항목에 액세스하려는 특정 폴더에 대한 권한을 제공하는 아래 코드를 사용합니다.

 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;
        }

참조 URL : https://stackoverflow.com/questions/2833959/how-to-find-windows-service-exe-path

반응형