C # intermediate-read the Windows Service Program Execution path through the registry,
I. Preface
Assume that our C # solution contains multiple program applications, such as Web applications, console programs, WPF program applications, and Windows service applications.
So how do these non-Windows Service applications find the execution path of the Windows Service Application in the code?
Ii. Text
Assume that the Windows service has been started and its name is SocketService.
Step 1: Check whether the locally running Windows Service has a service named SocketService;
Step 2: Read the Registry. All Windows Services Running locally appear in the registry. We can find the registry information by service name;
Step 3: Read the ImagePath of the registry information to obtain the execution path of the Windows service.
Using System. ServiceProcess; // Add reference
Public static string GetFilePathFromService (string serviceName) {try {ServiceController [] services = ServiceController. getServices (); var socketService = services. firstOrDefault (x => String. equals (x. serviceName, "SocketService"); if (socketService! = Null) {var key = Registry. LocalMachine. OpenSubKey (@ "SYSTEM \ CurrentControlSet \ Services \" + serviceName); if (key! = Null) {var serviceExePath = GetString (key. GetValue ("ImagePath"). ToString (); var folderPath = Path. GetDirectoryName (serviceExePath); if (! String. IsNullOrEmpty (folderPath) & Directory. Exists (folderPath) {return folderPath ;}}} catch (Exception ex) {return null ;}
3. End
Recently, I am a little busy. It is estimated that the last three chapters of Webgl will be delayed... Embarrassed.