In this slide we'll learn how to make Windows service application using c#. windows service application run automatically when our machine starts.
For More Details:
My Blog: https://aspcodehunter.blogspot.com
Youtube: http://www.youtube.com/RajuAhmedMarginal
Linkdin: https://www.linkedin.com/in/raju-ahmed-263475126
1. Create windows service application
(Step By Step)
In this tutorial we’ll create a windows service application. Suppose we’ve service which gives us
notification in a text file when it starts and stop. Let’s create the service.
1. Create a project with your visual studio.
2. Create windows service application
(Step By Step)
2. Now select Windows Service application as shown below.
3. Now add an installer.
3. Create windows service application
(Step By Step)
4. After selection “Add Installer”, a file named “ProjectInstaller.cs” will be added in your solutions.
And there will be to things like label named “serviceInstaller1” and “serviceProcessInstaller1”.
First select “serviceInstaller1” and go to properties. Then change the marked values as shown in
image.
5. Now right click on “serviceProcessInstaller1” and go to properties then change the values as shown
in image.
4. Create windows service application
(Step By Step)
6. Now Right click on your project and Create Public static class Named “Library.cs”. Like shown in
image. And paste the following code inside the class.
using System.IO;
public static void WriteErrorLog(Exception ex)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "LogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + ex.Source.ToString().Trim() + "; " +
ex.Message.ToString().Trim());
sw.Flush();
sw.Close();
}
catch
{
//nothting
}
}
public static void WriteErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "LogFile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
sw.Flush();
sw.Close();
}
catch
{
//nothing
}
}
5. Create windows service application
(Step By Step)
7. Now Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug> Create a
text file named “LogFile.txt”.
8. Now Expand your Service (Ex. Here is Service1.cs) and double click on Service1.cs and write the
code showed in image.
6. Create windows service application
(Step By Step)
9. Now Save all changes and build your application. And Run Visual Studio Command Prompt as
Administrator mode. Change the directory to application’s drive as showed the following image (Ex.:
here our directory is “G”) and hit enter key.
10. Now Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug> and
copy the directory from address bar. And then type cd <space> then paste the directory you copied
and hit enter key. As like the following image.
7. Create windows service application
(Step By Step)
11. Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug>
Then copy your service name (Ex. Here our service name is “MyFirstServiceApp.exe”).
Now type “installutil.exe” <space> paste your service name you copied (in this case our service
name is “MyFirstServiceApp.exe”) from Debug folder in your project as same as showed in
following picture And hit enter key.
8. Create windows service application
(Step By Step)
12. If everything are ok then you will get this type of result in your cmd and it means your service is
ready to work.
9. Create windows service application
(Step By Step)
13. Now navigate to windows services from start menu and run is as administrator.
10. Create windows service application
(Step By Step)
14. Now follow the find your services (i.e.: Here our service Name is “MyFirstService” ) and click on
it then click on Start from left side of the service window.
11. Create windows service application
(Step By Step)
15. Right Click on your project > then select “Open Folder in File Explorer” > bin> Debug> and open the
text file Name “LogFile.txt”. You will see the following text has written in your text file. That means
your service is working as your expectation. Close the text file.
12. Create windows service application
(Step By Step)
16. Now once again go to service as described in 13 step. And stop your service as shown in picture
bellow.
17. Open your text file named “LogFile.txt” as described in step 15. Now you will see another line has
added in your text file which tells your service has stopped as same as following picture.
================================ (~_~)(~_~)(~_~)================================