Home > other >  C# Visual Studio Executable cannot read files within same folder
C# Visual Studio Executable cannot read files within same folder

Time:03-06

Application info: Visual Studio 2019 .NET 5.0 Windows Form application.

I'm making a App.Config file that will store user specific variables in XML format. The Executable will read the file and pull the information. I made sure to copy everything from the debug folder to the new folder (C:\Program Files (x86)\Test)

It works on my end but for half of the end users the program cannot read the config file. The program loads but the config file isn't being read.

Is there a security setting I'm missing on the folder itself? I've ran the .exe file as administrator and it still isn't finding the file that are in the same Test folder.

Code in case it matters (I think it's a security issue not code issue):

            XDocument xdocument = XDocument.Load("Application.config");
            IEnumerable<XElement> items = xdocument.Root.Elements();
            foreach (var item in items)
            {
                Configs.Add(item.Name.ToString(), item.Value.ToString());
            }

            Logging.WriteToLog("Config File:");
            foreach (string item in Configs.Keys)
            {
                Logging.WriteToLog(item   ": "   Configs[item].ToString());
            }

            if(!ReadBoards())
            {
                return false;
            }

            return true;

XML File named Application.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <LogFilePath>C:\Program Files (x86)\Test\Logs\</LogFilePath>
<Email>[email protected]</MondayEmail>
</configuration>

CodePudding user response:

Maybe in that case, it would be good idea to check the executable directory path runtime by alert(or message box) where executable runs on. So I suggest that you check the executable path runtime before loading application config file. If then, you might be able to address the issue I think.

CodePudding user response:

Somehow the test folder that was created didn't have the right security permissions and wouldn't accept the permissions when added. The resolution was deleting the test folder and manually creating it and setting up the security.

  • Related