Javed Ahmad

Javed Ahmad

  • 1.4k
  • 194
  • 2.7k

Windows Services ( Error occurred installation phase on Local System)

Feb 1 2023 12:08 PM

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;
            }
        }
    }
}
 

 


Answers (4)