Tuesday, November 2, 2010

Beginning Programming: File Listener Application Part I

Today is 11/2/2010

At my job in 2006 or somewhere around there. Every election year during the primary and general election season, contribution reporting is required by candidates. The law states that contributions must be made public within 24 hour by our agency.

Well, three times a day I was told when the pdf file containing the contributions were ready to upload to the server. I would manually move the files from the file server over to the web server. After awhile I found this process disturbing. Not in the sense that the act was disturbing, but I'd get into programming large projects and in the middle of writing an algorithm I'm disrupted by someone asking me to post the new pdf documents.

I came up with a solution. Create a console app that runs and listens to the directory for me. It's a simple application that utilizes the .NET File System Watcher Event.

Basically what this event does is listen for changes in a given directory. You can drill down to specific file type or file name. I did both.

In my 2010 release, I was tired of manually launching the console application and seeing this large dos window on my machine. So in this version, which I will demonstrate here, I will create the same console application in a windows form.

The purpose of doing it that way is so that the application hides in the system tray and pops up a notification when an event fires in that directory. I will explain to you what's going on.

One of the things that I find less in the market of programming is learning to port your skills to multiple interfaces. A programmer needs to know how to write a console application and use the same skills to write a desktop and web application. I will show you how to do all three with very little code change.

The overall goal is to build a full blow application called a file listener, which listens to a directory and copy files from one location to another. We will do that only using two platforms, desktop and console. The web is a little more complicated because the user rights and file access is different than console and desktop. But they can be overcame by some modifications to the file systems permissions. But that's a different discussion.

The first thing you need to know is that programming is an iterative process. You build one piece, test it. Build the next piece, test it and then have all these pieces. The next step is connecting them together. Some programmers call them modules, classes, functions, methods, but they are all objects and really the name depends on the programmers environment. Once you know the theory behind how a function, method, class, object and etc works, then you will be able to reverse engineer any language.

The first step is to build the application in modules. Let's start.
Open Visual Studio.

You want to collect a couple of things before getting started:
You need 3 icons - after collecting them set them aside for the main application:
1. Application icon - for the system tray
2. About form icon - just looks good
3. Preferences form icon - same thing, looks good

You will also need to create a image about 175 width x 318 height: This will be for your About Form's System.Drawing.Bitmap on the left side of the about.cs form. You can leave the default provided by Microsoft, but that's no fun.

Okay!

I'm an educator, and I understand the importance of lowering learning curves to make sure a student learns. To make this simple we will build our application in modules and then create our main application bringing all the pieces together.

THE PROJECT:
Mod One: The Listener

The listener is the key piece of our application. We will first build the listener with no validation or extra code, such as, error handling so that you can see how the listener works.

1. Create a new console application called thelistener.
2. We need to add several using statements to the beginning of our form.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Threading;

namespace thelistener
{
static class Program
{
static void Main()
{

}
}
}

FOR THOSE NEW TO CONSOLE APPS
You can skip this section if you already know how this works. If not read below:
A console application runs in a dos window. When the application is compiled, it is compiled as an exe application with a default icon that looks something like this:

The name of the compiled executable file will be named after your projects file name. However, once compiled, you can rename the file to any name you like. One of the problems that you will run into when coding console applications and testing them is if you do not have code written in your console application to stop the console window from disappearing after it has ran your program, it will go away.

This is pretty bad when you have data printing to the console window, such as error messages. To solve that problem we will put a Keyboard read method in the code, which will wait for any key to be pressed before continuing and then exiting.

Before we do that, I want to show you how quickly the console window appears and disappears. Run your program by pressing F5 on your keyboard.

Now lets stop the window from going away until a key is pressed.
Let's add a new function or method to our program called Run.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Threading;

namespace thelistener
{
static class Program
{
static void Main()
{

}
public static void Run()
{

}
}
}


Okay, time to think like a programmer: When the application launches, it first looks for the Main() method. When it finds main, it looks between it's code block { } and executes any code there. Currently, there isn't any code. You can add functions or methods all day, if you never call them, your program is useless.

We added a function or method called Run(), then we need to tell main to call run by adding its method name to Main()

static void Main()
{
Run();
}

Currently the run methods code block looks like this:

public static void Run()
{

}

Which means it will not do anything because there is nothing there. So the console window will show and go away just as before. Our goal is for our application to stop until we press a key on our keyboard.

The Console has several methods to output text to the screen. A few of those is called Write(), WriteLine() and Read(). What makes them different? it depends on your needs. One keeps the cursor on the same line, while the other moves the cursor one line down and the last listens for keyboard strokes.

Add the following:

public static void Run()
{
Console.WriteLine("");
}

We just told the program to write to the console window the string "" of blank text and then move one line down. But the program still closes.

It wouldn't make sense in adding a string of text between the quote marks if we don't get a chance to see them. Let's stop the console window from closing first, then add some text.

Add the following:

public static void Run()
{
Console.WriteLine("Press q to quit the sample program");
Console.Read();
}
Now run the program and bam! You can see the line Press q to quit the sample program

SUMMARY
Let's recap. A console application straight out of the box, a new program, will not remain visible on the screen when first run. That's in any programming language. You need to tell the program to stop and wait for instructions. In C# there is a Console method called Read() that waits for a keyboard event. When they keyboard is pressed it will execute and continue running the program. If there is nothing to do, the console window will disappear.

There are two other methods we discussed, the Write and WriteLine methods which outputs text to the console window. Usage depends on your needs. Write doesn't move the cursor, it sits at the same line as your line execution. However, WriteLine, executes your line and moves the cursor one line down.

I will return tomorrow and show you how to do the same thing in a Windows form. 11/2/2010

No comments: