Unable to Access Files in MQL5/Files Folder - Error Code 5002

 

I am encountering an issue with file access in my Expert Advisor (EA). Specifically, I am trying to place TXT files and DLL files into the MQL5/Files folder and have the EA access these files. However, I am receiving error code 5002, indicating that access to the files is denied.

Here are the details of the situation:

  • Error Code: 5002
  • Issue: The EA cannot access files located in the MQL5/Files directory.
  • Environment: This issue occurs both in my VPS environment and on my home computer.
  • Other EAs: EAs that require web authentication (DLL allowed) work correctly and authenticate without issues. However, my custom-created EA fails to operate.
  • Administrator Privileges: Running MetaTrader with administrator privileges does not resolve the issue.
  • File Visibility: I also encounter problems where the EA cannot find or list files within the Files directory.

Despite researching and troubleshooting various aspects, I haven't been able to find a solution.

Could someone please provide guidance on how to resolve this issue or suggest potential causes for this problem? Any help would be greatly appreciated.

 
Ryu--ro- Ito-:

I am encountering an issue with file access in my Expert Advisor (EA). Specifically, I am trying to place TXT files and DLL files into the MQL5/Files folder and have the EA access these files. However, I am receiving error code 5002, indicating that access to the files is denied.

Here are the details of the situation:

  • Error Code: 5002
  • Issue: The EA cannot access files located in the MQL5/Files directory.
  • Environment: This issue occurs both in my VPS environment and on my home computer.
  • Other EAs: EAs that require web authentication (DLL allowed) work correctly and authenticate without issues. However, my custom-created EA fails to operate.
  • Administrator Privileges: Running MetaTrader with administrator privileges does not resolve the issue.
  • File Visibility: I also encounter problems where the EA cannot find or list files within the Files directory.

Despite researching and troubleshooting various aspects, I haven't been able to find a solution.

Could someone please provide guidance on how to resolve this issue or suggest potential causes for this problem? Any help would be greatly appreciated.

Please, show some code (simplified).

Make sure you're trying to access correct MQL5/Files folder.

Make sure no other instances of software (MT/tester agents, external editors, etc) locks the files already.

Check antiviral software and system-level access rights for the folder (even admin can not have required rights to a folder, if it's created in a peculiar manner, and should retake ownership in order to access such files).

 
Stanislav Korotky #:

Please, show some code (simplified).

Make sure you're trying to access correct MQL5/Files folder.

Make sure no other instances of software (MT/tester agents, external editors, etc) locks the files already.

Check antiviral software and system-level access rights for the folder (even admin can not have required rights to a folder, if it's created in a peculiar manner, and should retake ownership in order to access such files).

Thank you for your response, Stanislav. I appreciate your suggestions. Here's a simplified version of the code that demonstrates the issue:


//+------------------------------------------------------------------+
//|                  SimplifiedFileAccessTest.mq5                    |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      "https://www.yourwebsite.com"
#property version   "1.00"
#property strict

#define FILE_NAME "test_file.txt"

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("File operations allowed: ", (MQLInfoInteger(MQL_FILE_ALLOW) ? "Yes" : "No"));
   Print("Full file path: ", TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\" + FILE_NAME);
   
   if(!FileIsExist(FILE_NAME))
   {
      int handle = FileOpen(FILE_NAME, FILE_WRITE|FILE_TXT);
      if(handle != INVALID_HANDLE)
      {
         FileWriteString(handle, "Test content");
         FileClose(handle);
         Print("Test file created successfully");
      }
      else
      {
         Print("Failed to create test file. Error code: ", GetLastError());
      }
   }
   
   int readHandle = FileOpen(FILE_NAME, FILE_READ|FILE_TXT);
   if(readHandle != INVALID_HANDLE)
   {
      string content = FileReadString(readHandle);
      FileClose(readHandle);
      Print("File content: ", content);
   }
   else
   {
      Print("Failed to read file. Error code: ", GetLastError());
   }
   
   return(INIT_SUCCEEDED);
}


Additional information:


1. I've double-checked the file path, and it's correct: `TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL5\\Files\\"`.

2. I've ensured no other software is locking the files. This issue persists even after a system restart.

3. Regarding antivirus software, I'm only using the built-in Windows Security. I've turned off App & Browser control, but I'm not sure if there are any specific settings I need to disable within Windows Security to resolve this issue.

4. Regarding system-level access rights, I've granted full permissions to the MetaTrader installation folder through its properties. However, I'm not entirely sure what specific system-level access rights I should be checking or modifying beyond this.


The EA consistently fails with error code 5002 when trying to create or read the file. I've also tried using the common folder (`FILE_COMMON` flag), but the result is the same.


Could you provide more specific guidance on what to check or modify in terms of Windows Security settings or system-level access rights? Are there any additional debugging steps you'd recommend given this information?

 
Ryu--ro- Ito- #:

The EA consistently fails with error code 5002 when trying to create or read the file. I've also tried using the common folder (`FILE_COMMON` flag), but the result is the same.

Could you provide more specific guidance on what to check or modify in terms of Windows Security settings or system-level access rights? Are there any additional debugging steps you'd recommend given this information?

The script looks ok, except for MQL_FILE_ALLOW constant, which is missing in the official docs - I never used it before and don't know if it may have side-effects.

You can check system-level access rights for a folder by opening context menu for it, then Properties, Security.

 
Stanislav Korotky #:

The script looks ok, except for MQL_FILE_ALLOW constant, which is missing in the official docs - I never used it before and don't know if it may have side-effects.

You can check system-level access rights for a folder by opening context menu for it, then Properties, Security.

There is no such thing as MQL_FILE_ALLOW on MQL5, it doesn't even compile.

 
Stanislav Korotky #:

The script looks ok, except for MQL_FILE_ALLOW constant, which is missing in the official docs - I never used it before and don't know if it may have side-effects.

You can check system-level access rights for a folder by opening context menu for it, then Properties, Security.

Thank you for your response. I have removed the use of MQL_FILE_ALLOW as it was indeed an error. When I use the common folder ( FILE_COMMON ), I am able to read the file successfully.

However, despite granting full access permissions to the File folder located under the same directory as the EA, I still encounter issues. I have also checked the permissions for the parent directories, and they all have full access as well.

Given that I can access the common folder, it seems clear that the problem lies more with permission settings than with security restrictions. Since full access has already been granted, I’m unsure where else to look to resolve this issue. Any additional guidance on what I might be overlooking would be greatly appreciated.

 

Hi

Maybe those are not the proper txt files you are trying to read.

If they have any different format, you should add length of string to the FileReadString or perhaps you have some different coding in the txt files try to save them in Unicode and then read them in the script.

Have a nice day

 
Ryu--ro- Ito- #:

However, despite granting full access permissions to the File folder located under the same directory as the EA, I still encounter issues. I have also checked the permissions for the parent directories, and they all have full access as well.

Given that I can access the common folder, it seems clear that the problem lies more with permission settings than with security restrictions. Since full access has already been granted, I’m unsure where else to look to resolve this issue. Any additional guidance on what I might be overlooking would be greatly appreciated.

Make sure you gave full premissions for the same user which executes MT5 (can be seen in the task manager).

Also check the way you start MT5. For example, for the portable mode it's stated in the docs:

To run the platform in the Portable mode, the following conditions must be met:

  • If the platform is installed in the Program Files folder, the user must have administrator rights on the computer. In addition, UAC (User Account Control) must be disabled in the operating system.
  • If the platform is installed in any other folder, the user must have permission to write data to that folder.

Platform Start - For Advanced Users - Getting Started - MetaTrader 5 Help
  • www.metatrader5.com
After installation, a group of programs of the trading platform is added to the Start menu, and the program shortcut is created on the desktop. Use...
 

maybe you can also check the solution in this thread as a last resort (if still encountering problems)

https://www.mql5.com/en/forum/471679

File cannot be opened 5004
File cannot be opened 5004
  • 2024.08.19
  • Curo Jimenes
  • www.mql5.com
I have read meny threads and docs, followed them but non of them work...
 
Ryu--ro- Ito-:

I am encountering an issue with file access in my Expert Advisor (EA). Specifically, I am trying to place TXT files and DLL files into the MQL5/Files folder and have the EA access these files. However, I am receiving error code 5002, indicating that access to the files is denied.

Here are the details of the situation:

  • Error Code: 5002
  • Issue: The EA cannot access files located in the MQL5/Files directory.
  • Environment: This issue occurs both in my VPS environment and on my home computer.
  • Other EAs: EAs that require web authentication (DLL allowed) work correctly and authenticate without issues. However, my custom-created EA fails to operate.
  • Administrator Privileges: Running MetaTrader with administrator privileges does not resolve the issue.
  • File Visibility: I also encounter problems where the EA cannot find or list files within the Files directory.

Despite researching and troubleshooting various aspects, I haven't been able to find a solution.

Could someone please provide guidance on how to resolve this issue or suggest potential causes for this problem? Any help would be greatly appreciated.

Please post a screenshot of Windows explorer with your file visible in the MQL5/Files folder.
 
Marzena Maria Szmit #:

Hi

Maybe those are not the proper txt files you are trying to read.

If they have any different format, you should add length of string to the FileReadString or perhaps you have some different coding in the txt files try to save them in Unicode and then read them in the script.

Have a nice day

Thank you for your suggestion. However, my issue seems to be different. I'm encountering a "file not found" error rather than a read error. It's puzzling because the file is recognized and read correctly when accessed from the Common folder.