C#でWindowsコマンドプロンプトを使用してWindowsサービスをインストールするにはどうすればよいですか?
ステップ1-
新しいWindowsサービスアプリケーションを作成します。
ステップ2-
Windowsサービスを実行するには、インストーラーをインストールする必要があります。インストーラーは、インストーラーをサービスコントロールマネージャーに登録します。 Service1.cs[Design]とAddInstallerを右クリックします。
ステップ3-
ProjectInstaller.cs [Design]を右クリックして、ビューコードを選択します。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
namespace DemoWindowsService{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer{
public ProjectInstaller(){
InitializeComponent();
}
}
} F12を押して、InitializeComponentクラスの実装に移動します。インストール中のWindowsサービスの名前となるサービス名と説明を追加します。
private void InitializeComponent(){
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalService;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "My Demo Service";
this.serviceInstaller1.ServiceName = "DemoService";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
} ステップ4−
次に、Service1.csクラスのテキストファイルにログデータを書き込むロジックを以下に追加しましょう。
using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace DemoWindowsService{
public partial class Service1 : ServiceBase{
Timer timer = new Timer();
public Service1(){
InitializeComponent();
}
protected override void OnStart(string[] args){
WriteToFile("Service started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000;
timer.Enabled = true;
}
protected override void OnStop(){
WriteToFile("Service stopped at " + DateTime.Now);
}
private void OnElapsedTime(object source, ElapsedEventArgs e){
WriteToFile("Service recall at " + DateTime.Now);
}
public void WriteToFile(string Message){
string path = @"D:\Demo";
if (!Directory.Exists(path)){
Directory.CreateDirectory(path);
}
string filepath = @"D:\Demo\Log.txt";
if (!File.Exists(filepath)){
using (StreamWriter sw = File.CreateText(filepath)){
sw.WriteLine(Message);
}
} else {
using (StreamWriter sw = File.AppendText(filepath)){
sw.WriteLine(Message);
}
}
}
}
} ステップ5(インストール)-
次に、コマンドプロンプトを使用してWindowsサービスをインストールします。管理者としてコマンドプロンプトを開き、以下のコマンドを入力します。
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
Windowsサービスのexeファイルが存在するフォルダを開き、以下のコマンドを実行します。
InstallUtil.exe C:\Users\[UserName] source\repos\DemoWindowsService\DemoWindowsService\bin\Debug\ DemoWindowsService.exe
次に、Windowsアプリケーションメニューから[サービス]を開きます。
Windowsサービスがインストールされ、期待どおりに実行が開始されたことがわかりました。
以下の出力は、サービスが実行中であり、ログをテキストファイルに期待どおりに配線していることを示しています。
-
Windows 10 のコマンド プロンプトでコピーする方法
コマンド プロンプトは、Windows オペレーティング システムで最も強力なツールの 1 つです。ただし、Windows GUI では実現できないことを実行できるにもかかわらず、コマンド プロンプトは常に、ツール内でのコピーと貼り付けに問題があるようです。ユーザーが簡単に操作できるように、このガイドでは、コマンド プロンプトでコピーする方法と、必要に応じてコマンド ラインを貼り付ける方法について説明します。 Windows 10 のコマンド プロンプトでコピーする方法 すべてのアプリケーションと Windows 10 で、コピー、ファイル、テキストなどのために押したデフォルトの CTRL
-
Windows 10 のコマンド プロンプトでディレクトリを変更する方法
コマンド プロンプト ユーザーが多数のコマンドを実行し、さまざまなエラーをトラブルシューティングできるようにする Windows の素晴らしいユーティリティの 1 つです。これは、Microsoft ユーザー コミュニティでは一般に「CMD」ツールと呼ばれています。テキストベースのユーザー インターフェイスを使用して、高度な管理機能を実行できます。今日の記事では、CMD プロンプトでディレクトリを変更するプロセスを順を追って説明します。 (CMD). CMD は、コンピューターにディレクトリの変更への直接アクセスを提供します。それを行うのに役立つ 1 つのコマンドは cd です。 (