Convert Console App To Windows Service Question

J

Jack David

How would I convert the following console app to a windows service??

using System;

using System.IO;

using FileProcessor;



namespace DirectoryMonitorConsole

{

/// <summary>

/// Summary description for Class1.

/// </summary>

public class DirectoryMonitorConsole

{

private File_Processor objFileProcessor;

/// <summary>

/// Used as a test application for the Directory Monitor Service

/// </summary>

[STAThread]

public static void Main()

{

// Define the directory to monitor

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"c:\FTPIN\PSIFL";

//watcher.Path = @"d:\FTPIN\PSIFL";

// Define what to monitor

watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;

// Define file filter

watcher.Filter = "*.*";// look for any new file

// Define event handlers

watcher.Created += new FileSystemEventHandler(OnChanged);

// Begin watching the directory

watcher.EnableRaisingEvents = true;

// Wait for the user to quit the program

Console.WriteLine(@"Press q to quit this program");

while(Console.Read()!='q');

}


/// <summary>

/// Event handler for a new file put into the directory that is being
monitored

/// </summary>

/// <param name="source"></param>

/// <param name="e"></param>

private static void OnChanged(object source, FileSystemEventArgs e)

{

Console.WriteLine("File: {0} {1}!", e.FullPath, e.ChangeType);

// Get the name of the new file

// Make a reference to a directory

DirectoryInfo di = new DirectoryInfo(@"c:\FTPIN\PSIFL");

//DirectoryInfo di = new DirectoryInfo(@"c:\FTPIN\PSIFL");

// Get a reference for each file in the directory

FileInfo[] fi = di.GetFiles();

string strFileName = fi[0].ToString();

fi = null;

di = null;

File_Processor objFileProcessor = new
FileProcessor.File_Processor(strFileName);

objFileProcessor = null;

}

}

}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Just create a new project, and add the code to create the FileSystemWatcher
in the OnLoad event.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top