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 );
   }
}
}

From the MSDN documentation -

If you cut and paste a folder with files into a folder being watched, the FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.

To be notified that the contents of folders have been moved or copied into a watched folder, provide OnChanged and OnRenamed event handler methods...

From the MSDN documentation -

So, if I good understand, it mean, that I must use Changed. But can you explain me, how I can use it like condition, because I try everything, but I have not good result. Thanks

Isn't that C#?

I think you can do it like

If My.Computer.FileSystem.FileExists("path") Then
    My.Computer.FileSystem.MoveFile("originalpath","pathtomoveto")

Isn't that C#?

I think you can do it like

If My.Computer.FileSystem.FileExists("path") Then
    My.Computer.FileSystem.MoveFile("originalpath","pathtomoveto")

No, it is C++/CLI but this is good idea, thanks. Because I find now, that I musnt use FileWatcher, because when the file will be created, then this condition: if ( File::Exists( path ) ) will find it. So, I can use this condition to run a new program, too. Is it good idea?
Because, when this file will be created I want to start executeprocedure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.