Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร


ขั้นตอนที่ 1 -

สร้างแอปพลิเคชันบริการ windows ใหม่

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

ขั้นตอนที่ 2 -

สำหรับการเรียกใช้บริการ Windows คุณต้องติดตั้งโปรแกรมติดตั้ง ซึ่งลงทะเบียนกับ Service Control Manager คลิกขวาที่ Service1.cs[ออกแบบ] และ AddInstaller

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

ขั้นตอนที่ 3 -

คลิกขวาที่ ProjectInstaller.cs [ออกแบบ] และเลือกรหัสมุมมอง

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

ใช้ System;ใช้ System.Collections;ใช้ System.Collections.Generic;ใช้ System.ComponentModel;ใช้ System.Configuration.Install;ใช้ System.Linq;ใช้ System.Threading.Tasks;namespace DemoWindowsService{ [RunInstaller(จริง) ] ProjectInstaller คลาสบางส่วนสาธารณะ :System.Configuration.Install.Installer { ProjectInstaller สาธารณะ () { InitializeComponent (); } }}

กด F12 และไปที่การใช้งานคลาส InitializeComponent เพิ่มชื่อบริการและคำอธิบายซึ่งจะเป็นชื่อของบริการ windows ระหว่างการติดตั้ง

<ก่อน>โมฆะส่วนตัว InitializeComponent () { this.serviceProcessInstaller1 =System.ServiceProcess.ServiceProcessInstaller ใหม่ (); this.serviceInstaller1 =ใหม่ System.ServiceProcess.ServiceInstaller (); // // serviceProcessInstaller1 // this.serviceProcessInstaller1.Account =System.ServiceProcess.ServiceAccount.LocalService; this.serviceProcessInstaller1.Password =null; this.serviceProcessInstaller1.Username =null; // // serviceInstaller1 // this.serviceInstaller1.Description ="บริการสาธิตของฉัน"; this.serviceInstaller1.ServiceName ="DemoService"; // // ProjectInstaller // this.Installers.AddRange (ใหม่ System.Configuration.Install.Installer [] { this.serviceProcessInstaller1, this.serviceInstaller1});}

ขั้นตอนที่ 4 -

ตอนนี้ให้เราเพิ่มตรรกะด้านล่างเพื่อเขียนข้อมูลบันทึกในไฟล์ข้อความในคลาส Service1.cs

ใช้ System;ใช้ System.IO;ใช้ System.ServiceProcess;ใช้ System.Timers;namespace DemoWindowsService{ บริการบางส่วนสาธารณะบางส่วน :ServiceBase{ ตัวจับเวลาตัวจับเวลา =ตัวจับเวลาใหม่ (); บริการสาธารณะ1(){ InitializeComponent(); } ป้องกันแทนที่ โมฆะ OnStart(สตริง[] args){ WriteToFile("บริการเริ่มต้นที่ " + DateTime.Now); timer.Elapsed +=ElapsedEventHandler ใหม่ (OnElapsedTime); ตัวจับเวลาช่วงเวลา =5000; timer.Enabled =จริง; } ป้องกันแทนที่ โมฆะ OnStop(){ WriteToFile("บริการหยุดที่" + DateTime.Now); } โมฆะส่วนตัว OnElapsedTime (แหล่งที่มาของวัตถุ ElapsedEventArgs e) { WriteToFile ("การเรียกคืนบริการที่" + DateTime.Now); } เป็นโมฆะสาธารณะ WriteToFile (ข้อความสตริง) { เส้นทางสตริง =@"D:\Demo"; ถ้า (!Directory.Exists(เส้นทาง)){ Directory.CreateDirectory(เส้นทาง); } เส้นทางของไฟล์สตริง =@"D:\Demo\Log.txt"; if (!File.Exists(filepath)){ ใช้ (StreamWriter sw =File.CreateText(filepath)){ sw.WriteLine(Message); } } อื่น ๆ { ใช้ (StreamWriter sw =File.AppendText (filepath)) { sw.WriteLine (ข้อความ); } } } }}

ขั้นตอนที่ 5 (การติดตั้ง) −

ตอนนี้เราจะติดตั้งบริการ windows ของเราโดยใช้พรอมต์คำสั่ง เปิด commandprompt ในฐานะผู้ดูแลระบบและระบุคำสั่งด้านล่าง

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

เปิดโฟลเดอร์ที่มีไฟล์ exe บริการ windows ของเราและเรียกใช้คำสั่งด้านล่าง

InstallUtil.exe C:\Users\[ชื่อผู้ใช้]source\repos\DemoWindowsService\DemoWindowsService\bin\Debug\DemoWindowsService.exe

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

ตอนนี้เปิดบริการจากเมนูแอปพลิเคชัน windows

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร

เราพบว่าบริการ windows ของเราได้รับการติดตั้งและเริ่มทำงานตามที่คาดไว้

ผลลัพธ์ด้านล่างแสดงว่าบริการกำลังทำงานและเชื่อมต่อบันทึกไปยังไฟล์ข้อความตามที่คาดหมาย

จะติดตั้งบริการ windows โดยใช้พรอมต์คำสั่ง windows ใน C # ได้อย่างไร