I have a function that registers updates of an active order using the OnOrderUpdate event. It logs this information to a text file.
From time to time, the system gives me an I/O access error -> "The process cannot access the file XXXXX because it is being used by another process". How can I fix this?
Thanks
private void OnOrderUpdate(object sender, OrderEventArgs e)
{
Order order = e.Order;
registerOrderUpdate(order);
}
void registerOrderUpdate(Order order)
{
// Crear la cabecera del CSV si el archivo no existe
if (!File.Exists(filePath))
{
string header = getHeader ();
File.WriteAllText(filePath, header);
}
string csvLine = getCSVLine(order);
// Añadir la línea al archivo CSV
try
{
// Añadir la línea al archivo CSV
File.AppendAllText(filePath, csvLine);
}
catch (UnauthorizedAccessException ex)
{
// Manejo de errores específicos de permisos o accesos denegados
Print($"Error de acceso al archivo: {ex.Message}");
}
catch (IOException ex)
{
// Manejo de errores específicos de E/S (input/output)
Print($"Error de I/O al escribir en el archivo: {ex.Message}");
}
catch (Exception ex)
{
// Manejo genérico para cualquier otro tipo de excepción
Print($"Ocurrió un error inesperado al escribir en el archivo: {ex.Message}");
}
}

Comment