How to develop a windows services and how to install with example in .Net using C#
I have already created but not installing Error occured during the installation command prompt....
Error:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe -i D:\JAVED -WORK ZONE\Exercises\Window Services\WindowsService\bin\Debug\WindowsService.exe
Microsoft (R) .NET Framework Installation utility Version 4.8.9037.0
Copyright (C) Microsoft Corporation. All rights reserved.
Exception occurred while initializing the installation:
System.IO.FileNotFoundException: Could not load file or assembly 'file:///D:\JAVED' or one of its dependencies. The system cannot find the file specified..
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>
Windows Service code is below...:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.Threading;
namespace WindowsService
{
[RunInstaller(true)]
public partial class Service1 : ServiceBase
{
int ScheduleTime = Convert.ToInt32(ConfigurationSettings.AppSettings["ThreadTime"]);
public Thread Worker = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
ThreadStart start = new ThreadStart(Working);
Worker = new Thread(start);
Worker.Start();
}
catch (Exception)
{
throw;
}
}
private void Working()
{
while (true)
{
String Path = "D:\\Sample.text";
using (StreamWriter sw = new StreamWriter (Path,true))
{
sw.WriteLine(string.Format("Windows Service Called On :" + DateTime.Now.ToString("dd/mm/yyyy hh:mm ss tt")+""));
sw.Close();
}
Thread.Sleep(ScheduleTime * 60 * 1000);
}
}
protected override void OnStop()
{
try
{
if (Worker != null)
{
Worker.Abort();
}
}
catch (Exception)
{
throw;
}
}
}
}


Amit MohantyRank #112 · 20K reputationPosted Feb 1, 2023, 7:38 AM EST
Try this
InstallUtil.exe -i "D:\JAVED -WORK ZONE\Exercises\Window Services\WindowsService\bin\Debug\WindowsService.exe"Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 1, 2023, 7:13 AM EST
Hello Javed,
Please refer below articles:
https://www.c-sharpcorner.com/article/create-windows-services-in-c-sharp/
https://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/
Thanks
Naimish
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 1, 2023, 11:37 AM EST
To install a service you can also use below command.
sc create ServiceName binPath= "path\to\executable"Where:
example:
sc create MyService binPath= "D:\JAVED -WORK ZONE\Exercises\Window Services\WindowsService\bin\Debug\WindowsService.exe"using InstallUtil.exe:
Thanks
Naimish
Sachin SinghRank #66 · 26.2K reputationPosted Feb 1, 2023, 12:09 PM EST
Better follow two step process , first enter inside the directory then fire the util command
---and press enter--then--