C # writes the Windows service and sets it to "allow service to interact with the desktop"

Source: Internet
Author: User

This information comes from the network, I just summarize.

VS Create Project, project type Select Window Service

To create a setup program for a service

1. Return to the Design view of the Service1.
2: Click the background of the designer to select the service itself, not any of its contents.
3. In the Properties window, click the Add Installer link in the gray area below the list of properties.
By default, a component class that contains two installers is added to your project. Name the component
ProjectInstaller, which contains the Setup program for the service and the installation of the service associated process, respectively
Order.

4. Visit the Design view of the ProjectInstaller, and then click ServiceInstaller1.
5. In the Properties window, set the ServiceName property to MyNewService.
6: Set the StartType property to Automatic.
7. In the designer, select ServiceProcessInstaller1 (for Visual Basic projects),
or ServiceProcessInstaller1 (for Visual C # projects). Set the Account property to
LocalService. This will cause the service to be installed and running on the local service account. For more information, see
The Serviceprocessinstaller.account property.
Security Note the LocalService account acts as a non-privileged user on the local computer, to any remote service
The device displays anonymous credentials. Use other accounts with special care, so they have higher privileges and increase your
Be at risk of malicious code attacks.

set the service to "allow service to interact with the desktop"
To set the 7th step in the "Allow service to interact with desktop" section above: the account attribute must be set to LocalSystem, Then overload Onafterinstall in the ProjectInstaller class in ProjectInstaller.cs, with the following code:

[Runinstaller (true)] Public Partial classProjectInstaller:System.Configuration.Install.Installer { Public ProjectInstaller() {InitializeComponent (); }protected Override void Onafterinstall(IDictionary savedstate) {Try{Base.                Onafterinstall (savedstate); System.Management.ManagementObject MyService =NewSystem.Management.ManagementObject (string. Format ("Win32_service.name= ' {0} '", This. Serviceinstaller1.servicename)); System.Management.ManagementBaseObject Changemethod = Myservice.getmethodparameters ("Change"); changemethod["DesktopInteract"] =true; System.Management.ManagementBaseObject Outparam = Myservice.invokemethod ("Change", Changemethod,NULL); }Catch(Exception) {            }        }    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # writes the Windows service and sets it to "allow service to interact with the desktop"

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.