C # intermediate-read the Windows Service Program Execution path through the registry,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.