Real-time Monitoring of folder applications (using Windows Service in winform)

Source: Internet
Author: User
Requirement: Check whether a file is created for the folder after it is started (method: Use windowsservice and filesystemwatcher to monitor the folder)
1. Start with windowsservice and create a new windowservice file named logtransferservice. CS.
2. Drag a filesystemwatcher named filesystemwatcher1: listens to the file system change notification and triggers an event when the files in the directory or Directory change.
3. Set the listening folder in one way, the folder operation method, and the type of the listening file .........

// <Summary>
// Use filesystemwatcher to listen to the file system creation notification and trigger an event when the file changes.
/// </Summary>
Private void usingfilesystemwatcher ()
{
String watcherfilepath = "E: // pos_ws // pos_logtransferservice // updatelog ";
// Configure the audience listener
Filesystemwatcher1.path = watcherfilepath;
Filesystemwatcher1.policyfilter = policyfilters. filename | policyfilters. lastaccess;
Filesystemwatcher1.filter = "*. txt ";
// Check whether the Sub-object is missing
Filesystemwatcher1.includesubdirectories = false;

# Region events (events triggered when a file is created 4)
Filesystemwatcher1.created + = new filesystemeventhandler (filesystemwatchersponcreated );
# Endregion

}
4. # method for sending region events
/// <Summary>
/// When the file is created
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Void filesystemwatcherreceivcreated (Object sender, filesystemeventargs E)
{
Transfertype = new transfertype ();
// Post-renewal
String suffix = E. name. substring (E. name. lastindexof ('. ') + 1, E. name. length-e. name. lastindexof ('. ')-1 );
String filename = filesystemwatcher1.path + "//" + E. Name;
If (suffix = "TXT ")
Transfertype = transfertype. textfiletransfer;
Transfer (filename, transfertype );
}
# Endregion
5. check when the service is started:

/// <Summary>
/// Service Activation
/// </Summary>
/// <Param name = "ARGs"> </param>
Protected override void onstart (string [] ARGs)
{

// Use filesystemwatcher to listen on the file system
Usingfilesystemwatcher ();
// Send an email notification when the service is opened
Sendmsgaboutserver (msgsendertype. emailsender, transferservice. serverstart );
}
6. Install and uninstall the service
On the windowsservice page, right-click and add the installer to automatically generate servcieprocessinstall1 and serviceinstaller.
You can use serviceprocessinstall to set the access method: Account: LocalService, networkservice, loaclsystem, user
You can use serviceinstaller to set dispalyname (the displayed name after the service is installed), servicename, and starttype (Service Startup Mode: automatic or manual)
7. Generate a solution: Under the debug folder of the project, the. exe file of the Service will be generated.
Run the command to install the file: from Visual Studio Tools --> enter the command prompt ---> enter the generated. EXE path
Install: Enter the command: instalutil pos_logtransferservice.exe
Uninstall: Enter the command: installutil/u pos_logtransferservice.exe

Related Article

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.