No matter if I run the application as Administrator or not, I always get an System.UnauthorizedAccessException exception when trying to search for specific files within the "Program files" folder.
The code looks like this, and the exception occurs at System.IO.Directory.GetFiles(..)
//--- Find folders Application path
{
try
{
string searchfile = "terminal64.exe";
string searchdir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string[] files = System.IO.Directory.GetFiles(searchdir, searchfile, System.IO.SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
string result = files[i];
int p=result.IndexOf(searchfile);
if (p>0)
{
result = result.Substring(0, p);
AddTargetFolder(result);
}
}
}
catch
{
}
}
I am using Visual Studio 2019. The exception is thrown in the debugger (VS is executed as Administrator) as well as when executing the final .exe file.
Any help is much appreciated.