Hi, I want to move a file, when it is created. I have filewatcher and I know filemove function, but I dont know how I can create condition, that the file will be moved, when it is create. Can you help me please?
This is my source code:
#include "stdafx.h"
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;
public ref class Watcher
{
private:
static void OnChanged( Object ^ sender, FileSystemEventArgs^ e )
{
Console::WriteLine( "Súbor: {0} {1}", e->FullPath, e->ChangeType );
}
static void OnRenamed( Object ^ sender, RenamedEventArgs^ e )
Console::WriteLine( "Súbor: {0} bol premenovaný na {1}", e->OldFullPath, e->FullPath );
}
public:
[PermissionSet(SecurityAction::Demand, Name="FullTrust")] //???
int static run()
{
String^args = "C:\\...";
FileSystemWatcher^ watcher = gcnew FileSystemWatcher;
watcher->Path = args;
watcher->NotifyFilter = static_cast<NotifyFilters>(NotifyFilters::LastAccess |
NotifyFilters::LastWrite | NotifyFilters::FileName | NotifyFilters::DirectoryName);
watcher->Filter = "*.txt";
watcher->Changed += gcnew FileSystemEventHandler( Watcher::OnChanged );
watcher->Created += gcnew FileSystemEventHandler( Watcher::OnChanged );
watcher->Deleted += gcnew FileSystemEventHandler( Watcher::OnChanged );
watcher->Renamed += gcnew RenamedEventHandler( Watcher::OnRenamed );
watcher->EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console::WriteLine( "Press \'q\' to quit the sample." );
while ( Console::Read() != 'q' );
return 0;
}
};
int main()
{
Watcher::run();
[B]CONDITION???????[/B]
{
String^ path = "C:\\...\\test.txt";
String^ path2 = "C:\\...\\test.txt";
try
{
// Move the file.
File::Move( path, path2 );
Console::WriteLine( "{0} was moved to {1}.", path, path2 );
// See if the original exists now.
if ( File::Exists( path ) )
{
Console::WriteLine( "The original file still exists, which is unexpected." );
}
else
{
Console::WriteLine( "The original file no longer exists, which is expected." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
}