« Service et csharp » : différence entre les versions
Apparence
Ligne 54 : | Ligne 54 : | ||
= [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#install-the-service Install the service] = | = [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#install-the-service Install the service] = | ||
# Open {{boxx|Developer Command Prompt for VS}} with administrative credentials | # Open {{boxx|Developer Command Prompt for VS}} with administrative credentials | ||
<kode lang=' | <kode lang='dos'> | ||
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug | cd %UserProfile%\source\repos\MyService\MyService\bin\Debug | ||
installutil MyService.exe | installutil MyService.exe | ||
</kode> | </kode> |
Version du 11 février 2022 à 22:50
Links
Create the project
- File → New → Project
- Windows Service (.NET Framework)
- Rename Service1.cs to MyService.cs
- Double-click on MyService.cs to open the designer → Properties → ServiceName = MyService
Code
MyService.cs |
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{ }
protected override void OnStop()
{ }
}
|
Program.cs |
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
|
Installer
- Right-click on MyService.cs → View Designer
- Right-click on the background → Add Installer
- A new class ProjectInstaller has been added
- Right-click on ProjectInstaller.cs → View Designer
- Description = This service manages the ...
- DisplayName = My service
- StartType = Automatic
- Select serviceProcessInstaller1 → Properties → Account = LocalSystem
Build the service
- Right-click on MyService project → Properties
- Application tab → Startup object = MyService.Program
Install the service
- Open Developer Command Prompt for VS with administrative credentials
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug
installutil MyService.exe
|