PC Review


Reply
Thread Tools Rate Thread

Convert Console App To Windows Service Question

 
 
Jack David
Guest
Posts: n/a
 
      11th May 2004
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;

}

}

}


 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      11th May 2004
Just create a new project, and add the code to create the FileSystemWatcher
in the OnLoad event.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Jack David" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
>
> }
>
> }
>
> }
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Service or Console application Krish Microsoft Dot NET 6 19th Dec 2006 02:26 PM
Hybrid Windows Service + Console App Chris Mullins Microsoft Dot NET Framework 3 28th Sep 2006 07:00 PM
Convert Console App To Windows Service jabailo@texeme.com Microsoft Dot NET 3 23rd Jun 2005 10:35 PM
Windows service with console =?Utf-8?B?Um9iIFdoaXRl?= Microsoft Dot NET Framework 3 12th May 2005 09:55 AM
Architecture: Web Service as Management Console for Windows Service Vincent Mouton Microsoft Dot NET Framework 0 10th Nov 2003 10:00 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:42 AM.