How it works:
This service scans in a directory (C:\FILESERVICE\INBOX) by using the
.NET FileWatcher component (not really scans - it just checks for
created files)
when a file is created the service will check the extension of the file. If
the file has the extension .DBX then it will read the contents of the file and
communicate with the database (C:\FILESERVICE\DB\MyDB.mdb) and insert / update /
delete the records in the database. Then the file will be moved to the
subdirectory \BACKUP. When an error occurs the error will be logged in the
EventLog.
If an other file other than a DBX arrives in the inbox it will be moved
to the subdir \UNKNOWN.
If you look at the code, you can easy make the service to use with other kind
of files. and you can make an application (class) that handle those files.
Example extension
Mail could be used to send mails by using my code in "C#
mail console" so when you share the C:\FILESERVICE\Inbox for FTP , the
service will handle the incoming files...
First run
If the service is running then the first time it will check if the
directory's exists on the C:\ drive. if not, it will be created. The directory's
are:
- C:\FILESERVICE\DB
- C:\FILESERVICE\INBOX
- C:\FILESERVICE\INBOX\BACKUP
- C:\FILESERVICE\INBOX\UNKNOWN
In the zip file you can find 2 examples for database use, called USERS.DBX
and ARTICLES.DBX If you want to view those files open it with notepad. the
records are comma separated.
Next you have to do is copy the Access dababase file included in the zip file
and copy it to the C:\FILESERVICE\DB directory.
Then you can put a example of the file .DBX in the inbox dir, and the service
will handle the file. Open then the database file and check the records that are
inserted/updated / deleted.
Installing the service
First compile the application so you will have an executable (exe). For the
Visual Studio users, go to Start -> Microsoft VS.NET -> VS.NET Tools ->
VS.NET Command Prompt. You will see a command box (console) then go to the
directory where the exe is. then in that directory type
InstallUtil MyFileService.exe
This will install the service Once it correctly installed you can start the
service by typing
NET START TWINXBASE
Or you can go to Services in Configuration, look for the new service, start
it. To Remove the service, stop the service and type
InstallUtil /u MyFileService.exe
Remark
This is just a small example.. I will try to make the service later for
reading XML files for database use and others. But now you have an idea howto
create a service and more..
Enjoy.