Scenario: after Ranorex interaction with some desktop apps I need to find new created files and folder and copy them to another location.
I'm not c# developer, so I just copied some code and it woks fine at visual studio.
Here's code:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using System.IO;
namespace ConsoleApp1
{
class Program
{
private static string sourcePath;
private static Boolean stopFlag = false;
static void Main(string[] args)
{
Watch();
}
private static void Watch(string tempFolderPath)
{
sourcePath = tempFolderPath;
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.IncludeSubdirectories = true;
watcher.Path = tempFolderPath;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.exe";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Created += FileSystemWatcher_Created;
watcher.EnableRaisingEvents = true;
while (!stopFlag);
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
string newFileFullPath = e.FullPath;
string fName = Path.GetFileName(newFileFullPath);
try
{
File.Copy(newFileFullPath, Path.Combine("f:\\temp\\", fName));
}
// Catch exception if the file was already copied.
catch (IOException copyError)
{
Console.WriteLine(copyError.Message);
}
Console.WriteLine("new file is found and copied, stopping program");
stopFlag = true;
}
public void AdwareDetection(string tempFolderPath)
{
Watch(tempFolderPath);
}
}
}
1. Run App
2-8 mouse clicks
9. User code (posted above)
10-n mouse clicks.
Problem: program seems running cycled at while (!stopFlag); - I logged every line before and after - it stays here.

I suppose that this code at visual studio and at ranorex studio is run in some different ways, and maybe another way for file creation awaiting should be used.
Any suggestions?
