Windows Service simple application listening for files in folders

Source: Internet
Author: User
Tags dot net visual studio 2010

Windows Services are powerful. Such applications do not have a user interface and can run as the system starts. Any user message can be written into Windows event logs, you can also set the service as a local service without user restrictions. You can suspend the service and continue. However, if the service is for real-time monitoring, we recommend that you disable the service suspension function.

The advantage is a bunch of things. Let's take a look at the actual practice. The so-called practice is also learned from others' blogs. Here I will use a blog of fantastic dot net-use it. net Development of Windows Services. I am here to talk about some of the issues I encountered when creating a Windows service.

--------------------------------------------------------------------

First, install the Windows Service:

Install the Service Catalog to use the installutil.exe program, copy the path of the compiled service application, Enter cmd, find the directory where the service application is located, and run "installutil yourservicename.exe ".

The problem arises. If the system environment variable is not set, an error is reported here, prompting that the installutil command is unavailable. Configure the environment variable, the general directory is in "C: \ WINDOWS \ Microsoft. net \ framework \ v4.0.30319 ". If the project version is 4.0, set this path. If the project version is low, an error occurs when the published version is earlier than the application version.

If you do not need to worry about the problem above, click Start> All Programs> Microsoft Visual Studio 2010> Visual Studio Tools> Visual Studio command prompt (2010 )", later on.

Note that the re-compilation service must first uninstall the original Windows service and then install the re-compiled Windows service.

Next, we will talk about EventLog attribute settings:

The EventLog control is added to my project. It has an enableraisingevents attribute. Microsoft interprets it as "indicates whether the component monitors Event Log Changes". The default value is false, if it is set to true, the Windows service cannot be started after it is installed. The specific cause is unclear for the moment. It may be related to other settings. If you have any clear child shoes, please advise.

Finally, paste my source code for your sharing: (do not show the line number, for your convenience)

    

Private bool servicepaused = false;
/// <Summary>
/// Start the service
/// </Summary>
/// <Param name = "ARGs"> </param>
Protected override void onstart (string [] ARGs)
{
EventLog. writeentry ("this service is in:" + datetime. Now. tostring () + "started! ");

Filesystemwatcher. enableraisingevents = true; // start listening
Filesystemwatcher. includesubdirectories = true;

Filesystemwatcher. Changed + = new filesystemeventhandler (onfilechanged );
Filesystemwatcher. Created + = new filesystemeventhandler (onfilecreated );
Filesystemwatcher. Deleted + = new filesystemeventhandler (onfiledeleted );
Filesystemwatcher. renamed + = new renamedeventhandler (onfilerenamed );
}
/// <Summary>
/// Stop the service
/// </Summary>
Protected override void onstop ()
{

}


Private void onfilechanged (Object source, filesystemeventargs E)
{
      if (servicePaused == false)
           {
EventLog. writeentry (E. Name + "this file is in:" + datetime. Now. tostring () + "changed! ");
}
}

Private void onfilerenamed (Object source, renamedeventargs E)
{
If (servicepaused = false)
{
EventLog. writeentry (E. Name + "this file is in:" + datetime. Now. tostring () + "renamed! ");
}
}

Private void onfilecreated (Object source, filesystemeventargs E)
{
If (servicepaused = false)
{
EventLog. writeentry (E. Name + "this file is created in:" + datetime. Now. tostring () +! ");
}
}

Private void onfiledeleted (Object source, filesystemeventargs E)
{
If (servicepaused = false)
{
EventLog. writeentry (E. Name + "this file is in:" + datetime. Now. tostring () + "deleted! ");
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.