'C# Visual Studio Executable cannot read files within same folder
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>
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | leosun |