Loading...

How to Create and Install a Windows Service in C# Easy Tutorial


What is Windows Service:

Microsoft Windows services, that are also known as NT services, operates in the background and it enables you to create running applications that starts automatically at the Computer Boot time. You can pause and restart the Windows Services without showing any user interface.

  • Applications require some user interaction to execute.
  • Internet Information Server automatically starts without the user logging into the system.
  • You can run run it without any user logging in.
  • You need Windows NT Kernel to execute Windows Service.
  • The Windows NT Kernel is available in Windows NT/2000/ XP and Later Versions of Windows.
  • You can not execute it on Windows Me/95/98.
  • In Windows, the Services Option is found in the Control Panel -> Administrative Tools

How to Create a Windows Service:


  • Open the Windows Service Project in Visual Studio.

  • Right click in "WindowsService1 - Microsoft Visual Studio"  and click on "Add Installer (menu item)"
  • Click on "Service Process Installer" that is enabled above.
  • Go to its Properties Menu.
  • Change its Name
  • Change Account Type to "Local System".
  • Now Click on ServiceInstaller1, that is besides Service Process Installer.
  • So to its Properties and change Display Name.
  • Its StartType should be Manual so it will not run automatically at Boot Time.
  • Build Solution.
  • Go to the Debug Folder of its Visual Studio file and copy Path.

How to Install a Windows Service:


  • Go to "Visual Studio Command Prompt and run it as Administrator.
  • Enter the path of Windows Service and install it using Command "installutil/i [name of Windows Service]".

  • The Windows Service is installed.
  • You can see it in Control Panel -> Administrative Tools -> Services, where you can start and stop it manually.


You can add the following code in Code View of Windows Service to have a log file of its execution time and stopping time.

Code:



namespace WindowsService_lab9
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            FileStream fs = new FileStream(@"D:/LogFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sf = new StreamWriter(fs);

            sf.BaseStream.Seek(0, SeekOrigin.End);
            sf.WriteLine("/nThe Service in Started" + DateTime.Now.ToString());
            sf.Flush();
            fs.Close();
        }
        protected override void OnStop()
        {
            FileStream fs = new FileStream(@"D:/LogFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sf = new StreamWriter(fs);

            sf.BaseStream.Seek(0, SeekOrigin.End);
            sf.WriteLine("/nThe Service in Stopped" + DateTime.Now.ToString());
            sf.Flush();
            fs.Close();
        }
    }
}

Store Secret key in Registry to run your software on specified machine, Learn Here.

Hit Like Button if you understood, It helps a lot to keep us Motivated.

1 comments:

Post a Comment

Google+

 
TOP