I am trying to make a program for watching created and deleted files inside all USBs connected to a computer, my code is currently working well, until I copy a folder.
Example: I connect an USB drive I:/ . If I copy a file named file.mp4, its added to my db correctly. But if I copy it inside a folder as Movies/file.mp4, its not added the file either the folder.
What can be happening, or how can I get the file name even if it's inside a new folder??? I apreciate any suggestion.
Here is my Code:
public void StartWatchers()
{
ArrayList aFileWatcherInstance = new ArrayList();
ArrayList lstFolders = new ArrayList();
lstFolders.Add(@"I:/");
lstFolders.Add(@"J:/");
lstFolders.Add(@"K:/");
lstFolders.Add(@"L:/");
lstFolders.Add(@"M:/");
lstFolders.Add(@"N:/");
lstFolders.Add(@"O:/");
lstFolders.Add(@"P:/");
lstFolders.Add(@"Q:/");
lstFolders.Add(@"R:/");
lstFolders.Add(@"S:/");
lstFolders.Add(@"T:/");
lstFolders.Add(@"U:/");
lstFolders.Add(@"V:/");
foreach (string sMonitorFolder in lstFolders)
{
//Check if Directory Exisits
if (Directory.Exists(sMonitorFolder))
{
FileSystemWatcher oFileWatcher = new FileSystemWatcher();
oFileWatcher.InternalBufferSize = 65536;
//Set the path that you want to monitor.
oFileWatcher.Path = sMonitorFolder;
oFileWatcher.Filter = "*.*";
//Set the Filter Expression.
oFileWatcher.Deleted += new System.IO.FileSystemEventHandler(FileSystemWatcherDeleted);
oFileWatcher.Created += new System.IO.FileSystemEventHandler(FileSystemWatcherCreated);
oFileWatcher.EnableRaisingEvents = true;
oFileWatcher.IncludeSubdirectories = true;
//Add a new instance of FileWatcher
aFileWatcherInstance.Add(oFileWatcher);
}
}
}
public static bool IsFileReady(string filename)
{
// If the file can be opened for exclusive access it means that the file
// is no longer locked by another process.
try
{
using (FileStream inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
return inputStream.Length > 0;
}
catch (Exception)
{
return false;
}
}
void FileSystemWatcherCreated(object sender, System.IO.FileSystemEventArgs e)
{
while (!IsFileReady(e.FullPath))
{
}
if (IsFileReady(e.FullPath))
{
FileInfo fileInfo = new FileInfo(e.FullPath);
String letra = e.FullPath.Substring(0, 3);
if (connection.State != ConnectionState.Open)
connection.Open();
// Creamos una orden con la cadena de texto con la sentencia UPDATE
SQLiteCommand command = connection.CreateCommand();
command.CommandText = SQLUpdateArchivoCopiado;
// Añadimos los parámetros Name, Surname y UserId
command.Parameters.AddWithValue("ArchivoCopiado", e.Name);
command.Parameters.AddWithValue("Letra", letra);
// Ejecutamos la sentencia UPDATE y cerramos la conexión
command.ExecuteNonQuery();
// SUMAR EL TAMANO DEL ARCHIVO PARA OBTENER EL TOTAL COPIADO
SQLiteCommand commando = connection.CreateCommand();
commando.CommandText = SQLSetIncrementTotalCopiado;
var info = new FileInfo(e.FullPath);
var theSize = info.Length;
commando.Parameters.AddWithValue("TotalCopied", theSize);
commando.Parameters.AddWithValue("Letra", letra);
// Ejecutamos la sentencia UPDATE y cerramos la conexión
commando.ExecuteNonQuery();
connection.Close();
}
}
void FileSystemWatcherDeleted(object sender, System.IO.FileSystemEventArgs e)
{
FileInfo fileInfo = new FileInfo(e.FullPath);
String letra = e.FullPath.Substring(0, 3);
if (connection.State != ConnectionState.Open)
connection.Open();
// Creamos una orden con la cadena de texto con la sentencia UPDATE
SQLiteCommand command = connection.CreateCommand();
command.CommandText = SQLUpdateArchivoEliminado;
// Añadimos los parámetros Name, Surname y UserId
command.Parameters.AddWithValue("ArchivosEliminados", e.Name);
command.Parameters.AddWithValue("Letra", letra);
// Ejecutamos la sentencia UPDATE y cerramos la conexión
command.ExecuteNonQuery();
connection.Close();
}