3

I created a c# library file,that will go to local path for e.g(c:\test.txt) find the file and upload to ftp server.

For testing i just called the dll from console app but how i can run as a windows service,that will run continously ?

I suppose to run this dll as a service to monitor the c:\ folder if any file comes like "test.txt" then upload.

Thanks in Advance

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
Usher
  • 2,146
  • 9
  • 43
  • 81

2 Answers2

6

A DLL is "called" when you use it's functions. So if you have a DLL project with a few classes and a few functions inside your classes, simply add the DLL as a reference to your Service project and call the appropriate functions.

You may want to use this library for creating your Windows Service. The reason being, when you build a Windows Service using the Visual Studio template, the resulting build isn't really 'run'. The resulting build is a Windows Service that must be installed and then started using the Services Snap-In. But using Hoytsoft's custom Windows Service library, your service will install itself automatically and then start itself automatically - just like a regular Windows Form application.

To ensure your Service runs at startup, remember to configure your Service class to AutoStart (as explained in his CodeProject article). To ensure your Service runs continuously, even when the process is killed, you can add this handy registry hack which sets the Restart Service flags to Restart Immediately.

This Registry Hack == enter image description here

Registry Hack:

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\<YOUR_SERVICE_NAME_HERE>", true);
key.SetValue("FailureActions", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, Microsoft.Win32.RegistryValueKind.Binary);
key.Close();

Understand that any administrative user can still kill your process by simply disabling your service.

Jason
  • 6,878
  • 5
  • 41
  • 55
  • This is awesome Jason but am not sure the HoytSoft.dll run on my my test environment is 64 bit. – Usher Feb 27 '12 at 04:37
  • i haven't yet tested in my Test Environment before deploy there just wanna make sure it supports 64bit also. – Usher Feb 27 '12 at 06:35
  • I'm not sure, can you run it in a 64-bit sandbox to find out? – Jason Feb 27 '12 at 06:55
0

Check out the FileSystemWatcher class to watch for changes to a directory/file.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
  • 1
    thanks for you help but you might misunderstood,i got the answer from jason. – Usher Feb 27 '12 at 04:31
  • Ok, it just seemed like your goal was to monitor a directory, and that is what the FileSystemWatcher class does. – Chuck Savage Feb 27 '12 at 18:42
  • No i was given an example,i was suppose to monitor my database(used trigger earlier) but doen't work in my scenario ,so i changed to windows service to do that part. – Usher Feb 27 '12 at 23:47