m_FileWatcher.Created +=3D new
FileSystemEventHandler(this.OnFileChangedOrCreated);
m_FileWatcher.Changed +=3D new
FileSystemEventHandler(this.OnFileChangedOrCreated);
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
soudep
Hi Delf,
1). J'ai crée un variable global qui va stocker la chaîne contenant le nom du fichier qui sera "déplacé". Je l'appelle "strFileName" 2). Ensuite j'ajoute un évènement OnChanged qui fait la vérification suivante avant d'afficher dans la console : Si (le type de changement = changement ET la chaîne contenant le nom de fichier n'est pas le même que celui d'avant ) Alors Afficher le message
private static void OnChanged(object source, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed && !(strFileName.Equals(e.FullPath)) ) { Console.WriteLine("Fichier: " + e.FullPath + " " + e.ChangeType); strFileName = e.FullPath; } } 3). Il faudra peut-être remplacer la chaîne "strFileName" par un HashTable contenant tous les noms de fichiers qui seront crées et parcourir le HashTable pour faire les choses encore plus correctement ? Peut-être 1 HashTable pour les fichiers crées, 1 autre pour les fichiers modifiés, 1 autre pour ceux effacés et 1 autre pour ceux qui seront renommés ...
------------------------------------------------------- Copies-colles tout ça et testes --------------------------------------- using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; using System.Security.Permissions; using System.Collections;
namespace FileSystemWatcher1 { public class Watcher {
#region Declare Class Variables public static string strFileNameChanged = string.Empty; public static string strFileNameDeleted = string.Empty; public static string strFileNameCreated = string.Empty; #endregion
public static void Main() { Run(); }
#region Run Method [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public static void Run() { //Console.ReadLine(); 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("Usage: Watcher.exe (directory)"); Console.ReadLine(); }
// Create a new FileSystemWatcher and set its properties. FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:TEMP";//args[1]; /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Only watch text files. watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching. watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program. Console.WriteLine("Press 'q' to quit the sample."); while (Console.Read() != 'q') ; } #endregion
#region Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed && !(strFileNameChanged.Equals(e.FullPath))) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); strFileNameChanged = e.FullPath;
} else if (e.ChangeType == WatcherChangeTypes.Deleted && !(strFileNameDeleted.Equals(e.FullPath))) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); strFileNameDeleted = e.FullPath;
} else if (e.ChangeType == WatcherChangeTypes.Created && !(strFileNameCreated.Equals(e.FullPath))) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); strFileNameCreated = e.FullPath; } }
private static void OnRenamed(object source, RenamedEventArgs e) { // Specify what is done when a file is renamed. if (e.ChangeType == WatcherChangeTypes.Renamed) Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); } #endregion
}//fin Class }//fin NameSpace
- Anthony SEROU
Hi Delf,
1). J'ai crée un variable global qui va stocker la chaîne
contenant le nom du fichier qui sera "déplacé".
Je l'appelle "strFileName"
2). Ensuite j'ajoute un évènement OnChanged qui fait la
vérification suivante avant d'afficher
dans la console :
Si (le type de changement = changement ET
la chaîne contenant le nom de fichier n'est pas le
même que celui d'avant
)
Alors
Afficher le message
private static void OnChanged(object source,
FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed &&
!(strFileName.Equals(e.FullPath))
)
{
Console.WriteLine("Fichier: " + e.FullPath + " " +
e.ChangeType);
strFileName = e.FullPath;
}
}
3). Il faudra peut-être remplacer la chaîne "strFileName" par
un HashTable contenant tous les noms de fichiers qui seront crées et
parcourir le HashTable pour faire les choses encore plus
correctement ?
Peut-être 1 HashTable pour les fichiers crées, 1 autre
pour les fichiers modifiés, 1 autre pour ceux effacés et 1 autre pour
ceux qui seront renommés ...
------------------------------------------------------- Copies-colles
tout ça et testes ---------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
using System.Collections;
namespace FileSystemWatcher1
{
public class Watcher
{
#region Declare Class Variables
public static string strFileNameChanged = string.Empty;
public static string strFileNameDeleted = string.Empty;
public static string strFileNameCreated = string.Empty;
#endregion
public static void Main() { Run(); }
#region Run Method
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
//Console.ReadLine();
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("Usage: Watcher.exe (directory)");
Console.ReadLine();
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:\TEMP";//args[1];
/* Watch for changes in LastAccess and LastWrite times,
and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.CreationTime |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press 'q' to quit the sample.");
while (Console.Read() != 'q') ;
}
#endregion
#region Define the event handlers.
private static void OnChanged(object source,
FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed &&
!(strFileNameChanged.Equals(e.FullPath)))
{
// Specify what is done when a file is changed,
created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " +
e.ChangeType);
strFileNameChanged = e.FullPath;
}
else if (e.ChangeType == WatcherChangeTypes.Deleted &&
!(strFileNameDeleted.Equals(e.FullPath)))
{
// Specify what is done when a file is changed,
created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " +
e.ChangeType);
strFileNameDeleted = e.FullPath;
}
else if (e.ChangeType == WatcherChangeTypes.Created &&
!(strFileNameCreated.Equals(e.FullPath)))
{
// Specify what is done when a file is changed,
created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " +
e.ChangeType);
strFileNameCreated = e.FullPath;
}
}
private static void OnRenamed(object source, RenamedEventArgs
e)
{
// Specify what is done when a file is renamed.
if (e.ChangeType == WatcherChangeTypes.Renamed)
Console.WriteLine("File: {0} renamed to {1}",
e.OldFullPath, e.FullPath);
}
#endregion
1). J'ai crée un variable global qui va stocker la chaîne contenant le nom du fichier qui sera "déplacé". Je l'appelle "strFileName" 2). Ensuite j'ajoute un évènement OnChanged qui fait la vérification suivante avant d'afficher dans la console : Si (le type de changement = changement ET la chaîne contenant le nom de fichier n'est pas le même que celui d'avant ) Alors Afficher le message
private static void OnChanged(object source, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed && !(strFileName.Equals(e.FullPath)) ) { Console.WriteLine("Fichier: " + e.FullPath + " " + e.ChangeType); strFileName = e.FullPath; } } 3). Il faudra peut-être remplacer la chaîne "strFileName" par un HashTable contenant tous les noms de fichiers qui seront crées et parcourir le HashTable pour faire les choses encore plus correctement ? Peut-être 1 HashTable pour les fichiers crées, 1 autre pour les fichiers modifiés, 1 autre pour ceux effacés et 1 autre pour ceux qui seront renommés ...
------------------------------------------------------- Copies-colles tout ça et testes --------------------------------------- using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; using System.Security.Permissions; using System.Collections;
namespace FileSystemWatcher1 { public class Watcher {
#region Declare Class Variables public static string strFileNameChanged = string.Empty; public static string strFileNameDeleted = string.Empty; public static string strFileNameCreated = string.Empty; #endregion
public static void Main() { Run(); }
#region Run Method [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public static void Run() { //Console.ReadLine(); 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("Usage: Watcher.exe (directory)"); Console.ReadLine(); }
// Create a new FileSystemWatcher and set its properties. FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:TEMP";//args[1]; /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Only watch text files. watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching. watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program. Console.WriteLine("Press 'q' to quit the sample."); while (Console.Read() != 'q') ; } #endregion
#region Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed && !(strFileNameChanged.Equals(e.FullPath))) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); strFileNameChanged = e.FullPath;
} else if (e.ChangeType == WatcherChangeTypes.Deleted && !(strFileNameDeleted.Equals(e.FullPath))) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); strFileNameDeleted = e.FullPath;
} else if (e.ChangeType == WatcherChangeTypes.Created && !(strFileNameCreated.Equals(e.FullPath))) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); strFileNameCreated = e.FullPath; } }
private static void OnRenamed(object source, RenamedEventArgs e) { // Specify what is done when a file is renamed. if (e.ChangeType == WatcherChangeTypes.Renamed) Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); } #endregion
}//fin Class }//fin NameSpace
- Anthony SEROU
Delf
On 15 août, 13:20, soudep wrote:
1). J'ai crée un variable global qui va stocker la cha îne contenant le nom du fichier qui sera "déplacé". Je l'appelle "strFileName" 2). Ensuite j'ajoute un évènement OnChanged qui fait la vérification suivante avant d'afficher dans la console : Si (le type de changement = changement ET la chaîne contenant le nom de fichi er n'est pas le même que celui d'avant ) Alors Afficher le message
En fait notepad fait 2 modifications et PSPad 3... j'ai donc rajouté un membre de type DateTime.
Si j'intercepte 2 events "simultanés", je traite le 1° mais pas le 2° car délai < N secs (ici fixé à 2 secs). Le problème c'est que le traitement final (m_Callback), suite à l'event peut prendre lui aussi du temps.
=> je détache le traitement synchrone lié à l'event via le ThreadPo ol et je n'ai donc plus à gérer le délai du traitement.
using System; using System.IO; using System.Threading;
namespace Framework.IO.FileObjectMapper { public delegate void FileObjectMapperHandler(FileObjectMapperEventArgs e);
public class FileObjectMapper : IDisposable { private readonly string m_File = null;
public FileObjectMapper(string pFile, FileObjectMapperHandler pCallback) { if (pFile == null || pFile.Trim().Length == 0) { throw new ArgumentNullException("File", "The 'File' cannot be 'null' or empty'"); }
m_File = Path.GetFullPath(pFile);
if (pCallback == null) { throw new ArgumentNullException("Callback", "The 'Callback' cannot be 'null'"); }
m_FileWatcher.Created += new FileSystemEventHandler(this.OnFileChangedOrCreated); m_FileWatcher.Changed += new FileSystemEventHandler(this.OnFileChangedOrCreated); }
~FileObjectMapper() { this.Dispose(false); }
public void StartListening() { m_FileWatcher.EnableRaisingEvents = true; }
public void StopListening() { m_FileWatcher.EnableRaisingEvents = false; }
private void Dispose(bool pDisposing) { if (!m_IsDisposed) { if (m_FileWatcher != null) { m_FileWatcher.EnableRaisingEvents = false; m_FileWatcher.Dispose(); }
m_IsDisposed = true;
if (pDisposing) { GC.SuppressFinalize(this); } } }
#endregion } }
-- Delf
On 15 août, 13:20, soudep <pse...@gmail.com> wrote:
1). J'ai crée un variable global qui va stocker la cha îne
contenant le nom du fichier qui sera "déplacé".
Je l'appelle "strFileName"
2). Ensuite j'ajoute un évènement OnChanged qui fait la
vérification suivante avant d'afficher
dans la console :
Si (le type de changement = changement ET
la chaîne contenant le nom de fichi er n'est pas le
même que celui d'avant
)
Alors
Afficher le message
En fait notepad fait 2 modifications et PSPad 3... j'ai donc rajouté
un membre de type DateTime.
Si j'intercepte 2 events "simultanés", je traite le 1° mais pas le 2°
car délai < N secs (ici fixé à 2 secs).
Le problème c'est que le traitement final (m_Callback), suite à
l'event peut prendre lui aussi du temps.
=> je détache le traitement synchrone lié à l'event via le ThreadPo ol
et je n'ai donc plus à gérer le délai du traitement.
using System;
using System.IO;
using System.Threading;
namespace Framework.IO.FileObjectMapper
{
public delegate void
FileObjectMapperHandler(FileObjectMapperEventArgs e);
public class FileObjectMapper : IDisposable
{
private readonly string m_File = null;
public FileObjectMapper(string pFile, FileObjectMapperHandler
pCallback)
{
if (pFile == null || pFile.Trim().Length == 0)
{
throw new ArgumentNullException("File", "The 'File'
cannot be 'null' or empty'");
}
m_File = Path.GetFullPath(pFile);
if (pCallback == null)
{
throw new ArgumentNullException("Callback", "The
'Callback' cannot be 'null'");
}
m_FileWatcher.Created += new
FileSystemEventHandler(this.OnFileChangedOrCreated);
m_FileWatcher.Changed += new
FileSystemEventHandler(this.OnFileChangedOrCreated);
}
~FileObjectMapper()
{
this.Dispose(false);
}
public void StartListening()
{
m_FileWatcher.EnableRaisingEvents = true;
}
public void StopListening()
{
m_FileWatcher.EnableRaisingEvents = false;
}
1). J'ai crée un variable global qui va stocker la cha îne contenant le nom du fichier qui sera "déplacé". Je l'appelle "strFileName" 2). Ensuite j'ajoute un évènement OnChanged qui fait la vérification suivante avant d'afficher dans la console : Si (le type de changement = changement ET la chaîne contenant le nom de fichi er n'est pas le même que celui d'avant ) Alors Afficher le message
En fait notepad fait 2 modifications et PSPad 3... j'ai donc rajouté un membre de type DateTime.
Si j'intercepte 2 events "simultanés", je traite le 1° mais pas le 2° car délai < N secs (ici fixé à 2 secs). Le problème c'est que le traitement final (m_Callback), suite à l'event peut prendre lui aussi du temps.
=> je détache le traitement synchrone lié à l'event via le ThreadPo ol et je n'ai donc plus à gérer le délai du traitement.
using System; using System.IO; using System.Threading;
namespace Framework.IO.FileObjectMapper { public delegate void FileObjectMapperHandler(FileObjectMapperEventArgs e);
public class FileObjectMapper : IDisposable { private readonly string m_File = null;
public FileObjectMapper(string pFile, FileObjectMapperHandler pCallback) { if (pFile == null || pFile.Trim().Length == 0) { throw new ArgumentNullException("File", "The 'File' cannot be 'null' or empty'"); }
m_File = Path.GetFullPath(pFile);
if (pCallback == null) { throw new ArgumentNullException("Callback", "The 'Callback' cannot be 'null'"); }
m_FileWatcher.Created += new FileSystemEventHandler(this.OnFileChangedOrCreated); m_FileWatcher.Changed += new FileSystemEventHandler(this.OnFileChangedOrCreated); }
~FileObjectMapper() { this.Dispose(false); }
public void StartListening() { m_FileWatcher.EnableRaisingEvents = true; }
public void StopListening() { m_FileWatcher.EnableRaisingEvents = false; }
public FileObjectMapper( string pFile, FileObjectMapperHandler pCallback) { if (pFile == null || pFile.Trim().Length == 0) { throw new ArgumentNullException("File", "The 'File' cannot be 'null' or empty'"); }
m_File = Path.GetFullPath(pFile);
if (pCallback == null) { throw new ArgumentNullException("Callback", "The 'Callback' cannot be 'null'"); }
if (bAgain) { Thread.Sleep(TimeSpan.FromMilliseconds(250)); } } while (bAgain);
#endregion
m_LastEvent = DateTime.Now;
m_Callback(new FileObjectMapperEventArgs(e)); }
}
#region [ IDisposable Members ]
/// <summary> /// Releases all resources used by the /// System.ComponentModel.Component. /// </summary>
public void Dispose() { this.Dispose(true); }
private bool m_IsDisposed = false;
private void Dispose(bool pDisposing) { if (!m_IsDisposed) { if (m_FileWatcher != null) { m_FileWatcher.EnableRaisingEvents = false; m_FileWatcher.Dispose(); }
m_IsDisposed = true;
if (pDisposing) { GC.SuppressFinalize(this); } } }
#endregion } }
Le do/while permet de tester si le fichier est accessible. Par exemple, si onupload un fichier et qu'un FileSystemWatcher est en écoute dessus, le OnCreated est levé alors que le fichier n'a pas été fermé -> exception...
Si vous avez des remarques, n'hésitez pas.
-- Delf
Ce qui donne au final :
using System;
using System.IO;
using System.Threading;
namespace Framework.IO.FileObjectMapper
{
public delegate void FileObjectMapperHandler(
FileObjectMapperEventArgs e);
public class FileObjectMapper : IDisposable
{
private readonly string m_File = null;
public FileObjectMapper(
string pFile, FileObjectMapperHandler pCallback)
{
if (pFile == null || pFile.Trim().Length == 0)
{
throw new ArgumentNullException("File",
"The 'File' cannot be 'null' or empty'");
}
m_File = Path.GetFullPath(pFile);
if (pCallback == null)
{
throw new ArgumentNullException("Callback",
"The 'Callback' cannot be 'null'");
}
if (bAgain)
{
Thread.Sleep(TimeSpan.FromMilliseconds(250));
}
}
while (bAgain);
#endregion
m_LastEvent = DateTime.Now;
m_Callback(new FileObjectMapperEventArgs(e));
}
}
#region [ IDisposable Members ]
/// <summary>
/// Releases all resources used by the
/// System.ComponentModel.Component.
/// </summary>
public void Dispose()
{
this.Dispose(true);
}
private bool m_IsDisposed = false;
private void Dispose(bool pDisposing)
{
if (!m_IsDisposed)
{
if (m_FileWatcher != null)
{
m_FileWatcher.EnableRaisingEvents = false;
m_FileWatcher.Dispose();
}
m_IsDisposed = true;
if (pDisposing)
{
GC.SuppressFinalize(this);
}
}
}
#endregion
}
}
Le do/while permet de tester si le fichier est accessible.
Par exemple, si onupload un fichier et qu'un FileSystemWatcher est en
écoute dessus, le OnCreated est levé alors que le fichier n'a pas été
fermé -> exception...
public FileObjectMapper( string pFile, FileObjectMapperHandler pCallback) { if (pFile == null || pFile.Trim().Length == 0) { throw new ArgumentNullException("File", "The 'File' cannot be 'null' or empty'"); }
m_File = Path.GetFullPath(pFile);
if (pCallback == null) { throw new ArgumentNullException("Callback", "The 'Callback' cannot be 'null'"); }
if (bAgain) { Thread.Sleep(TimeSpan.FromMilliseconds(250)); } } while (bAgain);
#endregion
m_LastEvent = DateTime.Now;
m_Callback(new FileObjectMapperEventArgs(e)); }
}
#region [ IDisposable Members ]
/// <summary> /// Releases all resources used by the /// System.ComponentModel.Component. /// </summary>
public void Dispose() { this.Dispose(true); }
private bool m_IsDisposed = false;
private void Dispose(bool pDisposing) { if (!m_IsDisposed) { if (m_FileWatcher != null) { m_FileWatcher.EnableRaisingEvents = false; m_FileWatcher.Dispose(); }
m_IsDisposed = true;
if (pDisposing) { GC.SuppressFinalize(this); } } }
#endregion } }
Le do/while permet de tester si le fichier est accessible. Par exemple, si onupload un fichier et qu'un FileSystemWatcher est en écoute dessus, le OnCreated est levé alors que le fichier n'a pas été fermé -> exception...