Someone went to a lot of trouble to create these examples so why don't they work? I created a VC++ .NET project and copied in the sample source code. It does not even compile. Here is some source code that does work.
#include
"stdafx.h"
#using
<mscorlib.dll>
using
namespace System;
using
namespace System::IO;
public
__gcclass Watcher
{
public
:
// Define the event handlers.
staticvoid OnChanged(Object* /*source*/, FileSystemEventArgs* e)
{
// Specify what is done when a file is changed, created, or deleted.
Console::WriteLine(S"File: {0} {1}", e->FullPath,
__box(e->ChangeType));
}
staticvoid OnRenamed(Object* /*source*/, RenamedEventArgs* e)
{
// Specify what is done when a file is renamed.
Console::WriteLine(S"File: {0} renamed to {1}", e->OldFullPath, e->FullPath);
}
};
int
main()
{
String* args[] = System::Environment::GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args->Length != 2)
{
// Display the proper way to call the program.
Console::WriteLine(S"Usage: Watcher.exe (directory)");
return 0;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher* watcher =
new FileSystemWatcher();
watcher->Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher->NotifyFilter =
static_cast<NotifyFilters>( NotifyFilters::LastAccess | NotifyFilters::LastWrite
| NotifyFilters::FileName | NotifyFilters::DirectoryName );
// Only watch text files.
//watcher->Filter = S"*.txt";
// Add event handlers.
watcher->Changed +=
new FileSystemEventHandler(0, Watcher::OnChanged);
watcher->Created +=
new FileSystemEventHandler(0, Watcher::OnChanged);
watcher->Deleted +=
new FileSystemEventHandler(0, Watcher::OnChanged);
watcher->Renamed +=
new RenamedEventHandler(0, Watcher::OnRenamed);
// Begin watching.
watcher->EnableRaisingEvents =
true;
watcher->set_IncludeSubdirectories(
true);
// Wait for the user to quit the program.
Console::WriteLine(S"Press \'q\' to quit the sample.");
while(Console::Read()!='q');
}