简体   繁体   English

如何找到windows服务exe路径

[英]How to find windows service exe path

I have a windows service and I need to create directory to store some info.我有一个 Windows 服务,我需要创建目录来存储一些信息。 The directory path must be relative to the windows service exe file.目录路径必须相对于 windows 服务 exe 文件。 How can get this exe file path ?如何获得这个exe文件路径?

Tip: If you want to find startup path of installed windows service, look here from registry .提示:如果要查找已安装的 Windows 服务的启动路径,请从注册表查看这里。

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ + ServiceName

There are keys about windows service有关于windows服务的键

To get path for service you can use Management object.要获取服务路径,您可以使用 Management 对象。 ref: 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参考: 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());
                }

Instead of using a directory relative to the executable, and therefore needing admin privileges, why not use the common application data directory, which is accessible through与其使用相对于可执行文件的目录,因此需要管理员权限,为什么不使用公共应用程序数据目录,它可以通过以下方式访问

Environment.GetFolderPath(SpecialFolder.CommonApplicationData)

This way your app doesn't need write access to its own install directory, which makes you more secure.这样您的应用程序就不需要对其自己的安装目录的写访问权限,这使您更加安全。

尝试这个

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

svchost.exe is the executable which runs your service which is in system32. svchost.exe 是运行在 system32 中的服务的可执行文件。 Hence we need to get to the module which is being run by the process.因此,我们需要访问进程正在运行的模块。

The default directory for a windows service is the System32 folder. Windows 服务的默认目录是 System32 文件夹。 In your service, though, you can change the current directory to the directory that you specified in the service installation by doing the following in your OnStart:但是,在您的服务中,您可以通过在 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: an even simpler method (but I haven't tested yet):编辑:一个更简单的方法(但我还没有测试过):

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

这对我有用

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

即使在默认为 system32 的 LocalSystem 帐户下运行,这也会返回正确的路径:

System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);

If you want to get access of Program Files folder or any other using programming you should use the below code which is provide rights to specific folder.如果您想访问 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM