title: How to search files in "Program files" Folder with C#?
body: <p>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 &quot;Program files&quot; folder.</p>
  <p>The code looks like this, and the exception occurs at System.IO.Directory.GetFiles(..)</p>
  <pre><code>        //--- Find folders Application path
          {
          try
              {
                  string searchfile = &quot;terminal64.exe&quot;;
                  string searchdir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                  string[] files = System.IO.Directory.GetFiles(searchdir, searchfile, System.IO.SearchOption.AllDirectories);
  
                  for (int i = 0; i &lt; files.Length; i++)
                  {
                      string result = files[i];
                      int p=result.IndexOf(searchfile);
                      if (p&gt;0)
                      {
                          result = result.Substring(0, p);
                          AddTargetFolder(result);
                      }
                  }
                      
              }
          catch
              {
  
              }
          }
  </code></pre>
  <p>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.</p>
  <p>Any help is much appreciated.</p>