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