DirectoryInfo
related stuff seems useless in your code.
In your code, there is no code that send SMS so it is not possible to tell you where the problem is. However, it would be very easy for you to put a breakpoint on the code that does send the SMS and see why a message is sent.
That being said, why would you check every 4 seconds? If you need such accuracy, would not using a FileSystemWatcher be more appropriate (
FileSystemWatcher Class (System.IO)[
^])?
I think that one problem with your code is that you add a item in the table every 4 seconds so you would send a lot of SMS if you send one for each row in the table. You probably need to remember if a SMS was sent and only sent one if no SMS where sent. Otherwise, you would have a lot of message to send.
Assuming that you send individual message to many people, you might also overflow your email server capacity depending how messages are sent. If so, then it might continue to send email long after the alert condition is gone.
Also some lines are not properly indented and blank lines seems somewhat arbitrary. It might not affect the output but a programmer should be a professional who care for details.
Finally, it seems that you don't know the DRY principle (
Don't repeat yourself - Wikipedia, the free encyclopedia[
^]). In fact, it is very easy to spot that the code for
abcPath
is very similar to the code for
defPath
. This is very bad as every changes you make for
abcPath
, you will have to repeat it for
defPath
and if you add a third path to check it would even get worst.
You function has more that one responsability. Have you ever read about SOLID principles (
SOLID (object-oriented design) - Wikipedia, the free encyclopedia[
^]). In your case, your code does not respect the SRP (
Single responsibility principle - Wikipedia, the free encyclopedia[
^]).
Another problem with your code is that you use hard-coded constant in your code for the duration. If you decide to change the duration (say 10 minutes instead of 5), you have to find each occurrence of hard-coded value 5 and see if that number is related to the duration. For large projects, it would be almost impossible to find all such values. Same thing for the 4000 ms of sleep time.
Any constant (except trivial case like 1 in some circumstances) should be declared as such. For example:
const int maxDelayMinutes = 5;
Also, when a time should be considered the same time, I would suggest you to use a variable to make the intent clear. It could avoid some subtle bugs if the time change between call to
DateTime.Now
and the code assume that time is exactly the same.
That kind of problem is much easier to debug using a debugger. From a question like this one, we can only guess what other code do (like the actual SMS sending) but if the problem is in the code not shown, how can we guess it?
Also some information like ensuring that time is correct is crucial. For example, if you mix local and UTC time in your code, then it might not work as intended. That kind of problem is very easy to figure out with a debugger. You would make a modification to the directory and then step in your code to see if
timediff
has the expected value. Usually, one would works in UTC time and only use local time for display purpose. In fact, that code will probably not work as expected when daylight saving time changes.