Home > Computers and Internet > how to create windows Services

how to create windows Services

 

Windows services are applications that run outside of any particular user context in Windows NT, Windows 2000, or Windows XP. The creation of services used to require expert coding skills and generally required C or C++. Visual Studio .NET now makes it easy for you to create a Windows service, whether you’re writing code in C++, C#, or Visual Basic. You can also write a Windows service in any other language that targets the common language runtime. This article walks you through the creation of a useful Windows service, then demonstrates how to install, test, and debug the service.

 

 

I am showing  one example that copy only new file from one directory and paste into another directory at every five sec.

 

Step 1. Create a new project ->Window Service

 

Step 2. Select Event log from toolbox and drag into project

 

Step 3.Include the mention namespaces into project if its not.

 

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Threading;

using System.IO;

 

 

Step 5.

 

Write a code OnStart() function

 

file = new StreamWriter( new FileStream("C:\\ServiceTest.log", System.IO.FileMode.Append ) );

                  file.WriteLine("Starting Service");

                  file.Flush();

                  eventLog1.WriteEntry("my service started");

 

 

                  if (Directory.Exists(dirPathIn)==false)

                  {

                        Directory.CreateDirectory(dirPathIn);

                  }

                  if (Directory.Exists(dirPathOut)==false)

                  {

                        Directory.CreateDirectory(dirPathOut);

                  }

                  process =new Thread(new ThreadStart(CopyAndPaste));

                  process.Start();

 

Write a code OnStop()

 

file.WriteLine("my service stoped");

                  file.Flush();

                  file.Close();

                  eventLog1.WriteEntry("my service stoped");

                  process.Suspend ();

 

 

 

 

Create a new function CopyAndPaste()

 

private void CopyAndPaste()

            {

                  while(true)

                  {

                        DirectoryInfo dirInfo=new DirectoryInfo(dirPathIn);

                        DirectoryInfo dirInfoOut=new DirectoryInfo(dirPathOut);

                        foreach (FileInfo di in dirInfo.GetFiles())

                        {

                              bool flag=false;

                              foreach(FileInfo diOut in dirInfoOut.GetFiles())

                              {

                                    if (diOut.Name==di.Name){flag=true;}

                              }

                              if (flag==false){File.Move(di.FullName,dirPathOut + @"\" + di.Name);}

                        }

                        Thread.Sleep(2000);

                  }

            }

 

Write the code in a class  publicly

 

private const string dirPathIn=@"c:\helloIn";

            private const string dirPathOut=@"c:\helloOut";

            Thread process=null;

            StreamWriter file =null;

 

In a Constructor method add the code after InitializeComponent() methods

 

 

if(!System.Diagnostics.EventLog.SourceExists("FileMoveSourse"))

                        System.Diagnostics.EventLog.CreateEventSource("FileMoveSourse",

                              "FileMove");

 

                  eventLog1.Source = "FileMoveSourse";

                  // the event log source by which

 

                  //the application is registered on the computer

 

                  eventLog1.Log = "FileMove";

                  // TODO: Add any initialization after the InitComponent call

 

 

 

 

Step 5.

Select  Servies cs file in design time  and click

Add Installer hyperlink.

 

Step 6.Select processServiceInstaller1, In the property windows change the Account Property to LocalSystem.

 

Step 7. Check How to Install Service

 

 

 

For more information logon at http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/

 

 

 

 

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: